Django index html page

Django Add Main Index Page

The main page will be the landing page when someone visits the root folder of the project.

Now, you get an error when visiting the root folder of your project:

Start by creating a template called main.html :

Main

  My Tennis Club  

My Tennis Club

Members

Check out all our members

Create new View

Then create a new view called main , that will deal with incoming requests to root of the project:

from django.http import HttpResponse from django.template import loader from .models import Member def members(request): mymembers = Member.objects.all().values() template = loader.get_template('all_members.html') context = < 'mymembers': mymembers, >return HttpResponse(template.render(context, request)) def details(request, id): mymember = Member.objects.get(id=id) template = loader.get_template('details.html') context = < 'mymember': mymember, >return HttpResponse(template.render(context, request)) def main(request): template = loader.get_template('main.html') return HttpResponse(template.render()) 

The main view does the following:

Add URL

Now we need to make sure that the root url points to the correct view.

Open the urls.py file and add the main view to the urlpatterns list:

from django.urls import path from . import views urlpatterns = [ path('', views.main, name='main'), path('members/', views.members, name='members'), path('members/details/', views.details, name='details'), ] 

The members page is missing a link back to the main page, so let us add that in the all_members.html template, in the content block:

Example

If you have followed all the steps on your own computer, you can see the result in your own browser: 127.0.0.1:8000/ .

If the server is down, you have to start it again with the runserver command:

Источник

Читайте также:  Python узнать свойства объекта
Оцените статью