0

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

Lotw
  • 423
  • 1
  • 4
  • 15
  • 1
    I don't understand what the problem is. Your URL patterns look OK - you should be able to access the index view on `http://localhost:8000/` and the store view on `http://localhost:8000/store/`. A common mistake is to use `url(r'^', ...)` (without the $), and then it matches `/` and `/store/`, but that's not an issue here. – Alasdair Apr 02 '19 at 13:44
  • 1
    Note that `render_to_response` is obsolete - use `render` instead, e.g. `return render(request, 'index.html')`. – Alasdair Apr 02 '19 at 13:46
  • what do you see when you navigate to: http://localhost:8000/store/ ? – Walucas Apr 02 '19 at 13:55
  • @ Alasdair I edit the render_to_response thank you :) @Walucas I get the following error page: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/store.html Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: ^$ [name='index'] ^store/$ [name='store'] ^admin/ ^static\/(?P.*)$ The current path, store.html, didn't match any of these. You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. – Lotw Apr 02 '19 at 14:00
  • That's why I was thinking it's in the urlspattern, like some kind of order mistake.. but maybe I'm wrong.. – Lotw Apr 02 '19 at 14:02

1 Answers1

1

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516