Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django how to set main page

Tags:

i want to set a main page or an index page for my app. i tried adding MAIN_PAGE in settings.py and then creating a main_page view returning a main_page object, but it doesn't work Also, i tries to add in the urls.py a declaration like

(r'^$', index), 

where indexshould be the name of the index.html file on the root (but it obviously does not work)

What is the best way to set a main page in a Django website?

thanks!

like image 311
dana Avatar asked Jul 08 '10 13:07

dana


People also ask

How do I set default page in Django?

Set up app folder's urls.py and html files In the same directory, you should have a file named views.py. We will create a function called index which is what makes the http request for our website to be loaded. Now, we've set it up such that http://127.0.0.1:8000/homepage will render the HTML template index.

How do I redirect one page to another in Django?

Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .


1 Answers

The new preferred way of doing this would be to use the TemplateView class. See this SO answer if you would like to move from direct_to_template.

In your main urls.py file:

from django.conf.urls import url from django.contrib import admin from django.views.generic.base import TemplateView  urlpatterns = [     url(r'^admin/', admin.site.urls),     # the regex ^$ matches empty     url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'),         name='home'), ] 

Note, I choose to put any static pages linke index.html in its own directory static_pages/ within the templates/ directory.

like image 158
ryanjdillon Avatar answered Sep 23 '22 01:09

ryanjdillon