Django_Basics_Interview_QA (1)
Django_Basics_Interview_QA (1)
Answer:
Answer: When you create a Django project, you get the following structure:
myproject/
manage.py # Command-line utility for Django
myproject/ # Main project directory
__init__.py # Marks directory as a Python package
settings.py # Configuration settings
urls.py # URL routing
wsgi.py # Entry point for WSGI servers
app_name/ # Django app (created separately)
models.py # Database models
views.py # View functions or class-based views
urls.py # App-specific URLs
templates/ # HTML templates
static/ # Static files (CSS, JS, images)
Answer: manage.py is a command-line utility that allows you to interact with your
Django project. Common commands include:
Answer: settings.py contains the configuration for the Django project, including:
Answer: urls.py defines URL patterns that map URLs to specific views. Example:
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
This maps the / (root) URL to home view and /about/ to about view.
Answer: Django models define database tables using Python classes. Example:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
Answer: Django ORM (Object-Relational Mapping) allows interaction with the database
using Python instead of SQL. Example:
12. What is the difference between GET and POST methods in Django?
Answer:
admin.site.register(Product)
def home(request):
return render(request, 'home.html', {'title': 'Home Page'})
Answer:
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
{% load static %}
<img src="{% static 'images/logo.png' %}" alt="Logo">
Answer: CSRF (Cross-Site Request Forgery) token is used to protect forms from attacks.
Example:
<form method="post">
{% csrf_token %}
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
class CustomMiddleware:
def __init__(self, get_response):
self.get_response = get_response
Answer: