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

5
  • 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. Commented Apr 2, 2019 at 13:44
  • 1
    Note that render_to_response is obsolete - use render instead, e.g. return render(request, 'index.html'). Commented Apr 2, 2019 at 13:46
  • what do you see when you navigate to: localhost:8000/store ? Commented Apr 2, 2019 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: 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<path>.*)$ 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. Commented Apr 2, 2019 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.. Commented Apr 2, 2019 at 14:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I do wonder, I can I get to the code where I can just get to every view? I now tried this: ``` urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^store.html/$', views.store, name='store'), url(r'^products.html/$', views.products, name='products'),``` and now its pasting everything together and gives an error: "The current path, store.html/products.html, didn't match any of these." Which makes sense as this isn't a path..
It sounds like you are clicking a link <a href="products.html">, which is missing a slash at the beginning. It should be <a href="/products.html">, or even better reverse the URL with <a href="{% url 'products' %}">
I would suggest you use URLs like /store/ or store.html. URLs like /store.html/ with both an extension and a trailing slash look very unusual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.