Reputation: 47
Currently working on an exercise from PythonCrashCourse2ndEd. (Ch.18, Ex.8), and i'm getting a SyntaxError: Invalid syntax on urls.py. (line 13 path('pizza/', views.index, name='pizza), I have tried importing the file directly to shell, and it gave me the same error.
urls.py
"""Defines URL patterns for pizzas"""
from django.urls import path
from . import views
app_name = 'pizzas'
urlpatterns = [
#Home page
path('', views.index, name='index')
#Page that shows all the pizzas.
path('pizza/', pizzas.index, name='pizza')
]
views.py
from django.shortcuts import render
from .models import Pizza
def index(request):
"""The home page for pizzas."""
return render(request, 'pizzas/index.html')
def pizzas(request):
"""Show all the pizzas"""
pizzas = Pizza.objects.all()
context = {'pizzas': pizzas}
return render(request, 'pizzas/pizzas.html', context)
pizzas.html
<!--Inherits from base.html-->
{% extends "pizzas/base.html" %}
{% block content %}
<p>Pizzas</p>
<ul>
{% for pizza in pizzas %}
<li>{{ pizza }}</li>
{% empty %}
<li>No pizzas have been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
Error
>>> import pizzas.urls
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\Heyale\OneDrive\Desktop\pizzeria\pizzas\urls.py", line 13
path('pizza/', views.index, name='pizza')
Upvotes: 0
Views: 1166
Reputation: 1370
I'm wondering if you also noted that it was just not the missing ,
alone for the urlpatterns
list. But also you have been trying to use a view that doesn't for your pizza path 'pizza/'
.
What you have is:
urlpatterns = [
#Home page
path('', views.index, name='index')
#Page that shows all the pizzas.
path('pizza/', pizzas.index, name='pizza')
]
What it should be:
urlpatterns = [
#Home page
path('', views.index, name='index'), # comma added here
#Page that shows all the pizzas.
path('pizza/', views.pazzas, name='pizza') # changed pizzas.index to views.pazzas
]
Upvotes: 1
Reputation: 1659
urlpatterns is an array , so you have to seperate path elements with comma :
urlpatterns = [
#Home page
path('', views.index, name='index'),
path('pizza/', views.index, name='pizza')
]
to get all the URL patterns in django shell enter :
import urls
def show_urls(urllist, depth=0):
for entry in urllist:
print(" " * depth, entry.regex.pattern)
if hasattr(entry, 'url_patterns'):
show_urls(entry.url_patterns, depth + 1)
show_urls(urls.urlpatterns)
Upvotes: 0
Reputation: 34
In urls.py you put the same path for index and pizza both. You are routing (def index) function for pizza.
You should do this, it will help you
from django.urls import path
from . import views
app_name = 'pizzas'
urlpatterns = [
#Home page
path('', views.index, name='index')
#Page that shows all the pizzas.
path('pizza/', views.pizza, name='pizza')
]
Upvotes: -1
Reputation: 36
You are missing the comma after first path and also closing bracket "]" in urlpatterns. It should be:
urlpatterns = [
#Home page
path('', views.index, name='index'),
#Page that shows all the pizzas.
path('pizza/', views.index, name='pizza')
]
Upvotes: 2