Writing Your First Django App, part 3: server broken after trying to edit URL

On part 3 of the tutorial, I got down to “Removing hardcoded URLs in templates”. I tried changing the name of the polls detail view to “custom”, but when I tried accessing the server using /custom, I got the following error:

Page not found (404)
Request Method:	GET
Request URL:	http://127.0.0.1:8000/polls/custom/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

polls/ [name='index']
polls/ custom/<int:question_id>/ [name='custom']
polls/ <int:question_id>/results/ [name='results']
polls/ <int:question_id>/vote/ [name='vote']
admin/
The current path, polls/custom/, didn’t match any of these.

I did some troubleshooting and ended up changing the function name for the view in views.py, but it didn’t resolve the error. I then changed a few other things (I can’t remember what exactly), it still didn’t work, then I ended up changing “custom” back to the original “detail”. However, now I get a different error when trying to access the server:

# Page not found (404)

|Request Method:|GET|
| --- | --- |
|Request URL:|http://127.0.0.1:8000/|

Using the URLconf defined in `mysite.urls`, Django tried these URL patterns, in this order:

1. polls/
2. admin/

The empty path didn’t match any of these.

Clearly I’m missing something, or I unwittingly edited something while troubleshooting that has broken access to the entire server.

Post your urls.py, your views.py and your template code in order to get better help.

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("<int:question_id>/", views.detail, name="detail"),
    path("<int:question_id>/results/", views.results, name="results"),
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

views.py

from django.http import HttpResponse
from django.http import Http404
from django.shortcuts import get_object_or_404, render

from .models import Question

'''
def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")'''

def index(request):
    latest_question_list = Question.objects.order_by("-pub_date")[:5]
    context = {"latest_question_list": latest_question_list}
    return render(request, "polls/index.html", context)
    #output = ", ".join([q.question_text for q in latest_question_list])
    return HttpResponse(template.render(context, request))

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, "polls/detail.html", {"question": question})


def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)


def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

templates

detail.html

{h1}{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

index.html

{h1}{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

The name “custom” is used within your Django code to create the url. You do not use that name as a url in your browser.

You should roll your changes back to the point where you were when you made this change.

2 Questions.

  1. I thought the whole point of doing this was to change the URL itself? If not, what is the purpose of doing this?
  2. How do I roll back to a previous state?

You need a version control system like git

The full description is at Removing hardcoded URLs in templates. Briefly, it’s exactly what the title describes, you’re removing full hard-coded urls from your templates. It does not change the urls themselves.

That depends upon whether you’re using a version control system like git, or if your editor / IDE maintains file history, or if your editor’s undo function tracks these recent changes back far enough.