Day 8 Tamplate Inheritance Home, About, Contact
Day 8 Tamplate Inheritance Home, About, Contact
Django project with a simple template inheritance setup for home.html, about.html, and contact.html.
Step-by-Step Guide
1. Create a Django Project: First, create a new Django project. Open your terminal and run:
2. Create a Django App: Inside your project directory, create a new Django app:
3. Configure Installed Apps: Add your new app to the INSTALLED_APPS list in myproject/settings.py:
INSTALLED_APPS = [
...
'myapp',
]
TEMPLATES = [
{
...
'DIRS': [BASE_DIR / 'templates'],
...
},
]
5. Create Templates Structure: Create the directory structure for your templates:
mkdir -p templates/base
mkdir -p templates/myapp
6. Create Base Template: Create a base template file base.html inside the templates/base/ directory:
{% block content %}
<h1>Welcome to the Home Page</h1>
<p>This is the home page of the website.</p>
{% endblock %}
{% block content %}
<h1>About Us</h1>
<p>This is the about page of the website.</p>
{% endblock %}
{% block content %}
<h1>Contact Us</h1>
<p>This is the contact page of the website.</p>
{% endblock %}
10. Set Up URLs: Configure the URLs for your views. In myproject/urls.py, include your app’s URLs:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
11. Create URL Config for the App: In myapp/urls.py, create URL patterns for your views:
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
path('contact/', views.contact, name='contact'),
]
def home(request):
return render(request, 'myapp/home.html')
def about(request):
return render(request, 'myapp/about.html')
def contact(request):
return render(request, 'myapp/contact.html')
Visit http://127.0.0.1:8000/ in your browser to see the home page. You can navigate to
http://127.0.0.1:8000/about/ and http://127.0.0.1:8000/contact/ to see the about and contact pages,
respectively.