Search code examples
djangopython-3.xdjango-templatesurl-pattern

Python - Django add multiple urlpatterns for multiple views of template


I'm very very new to Python 3 and Django and I get to the following problem: I use a standard Template and now how to set it up when there is 1 view. But I don't get the code right for multiple views. I currently run the page locally

At the moment I have tried to change different orders within urlpatterns, and they do work when only 1 url in in there, but I can't get the second one in

views.py

from django.shortcuts import render, render_to_response

# Create your views here.
def index(request):
    return render_to_response('index.html')

def store(request):
    return render_to_response('store.html')

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from myapp import views as views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns



urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^store/$', views.store, name='store'),
    url(r'^admin/', admin.site.urls)
]
urlpatterns += staticfiles_urlpatterns()

I would like the url pattern that lets me go to the index view and the store view

EDIT: Full code is shared via: https://github.com/lotwij/DjangoTemplate


Solution

  • The error in the comments shows you are going to http:/127.0.0.1:8000/store.html, but your URL pattern url(r'^store/$', ...) does not include the .html, so you should go to http:/127.0.0.1:8000/store/.

    The Django URL system uncouples the URL from the name of the template (sometimes the view doesn't even render a template!). You could change the regex to r'^store.html$ if you really want .html in the URL, but I find the URL without the extension is cleaner.