0

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')
3
  • 3
    You are missing a comma after path('', views.index, name='index') ... Commented Jun 23, 2021 at 12:41
  • I'm speechless, I must have looked over a hundred times, and was unable to spot that. Thanks a bunch! Commented Jun 23, 2021 at 12:46
  • 1
    Also missing ] at end of urlpatterns list, but I'm guessing that was a cut-and-paste error. Commented Jun 23, 2021 at 12:51

4 Answers 4

2

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')
]
Sign up to request clarification or add additional context in comments.

Comments

1

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
]

Comments

0

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)

Comments

-1

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')
]

Comments

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.