I don't understand why I am getting a 4040 error while it seems I have defiend everything correctly. The Ticket
model class has instances saved in the database so pk
from 1 to 5 also exists. The html
template is the simplest possible just to figure out why django cannot get and render the requested instance. Note that I can see the instances in the admin
page and also other paths like adding new ticket etc are working fine. Any help is appreciated:
the path/page that I am trying to get:
http://127.0.0.1:8000/ticket/1/
app: urls.py
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'ticket/<int:pk>/', views.ticket_detail, name='ticket_detail'),
]
project: urls.py
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('mmrapp.urls')),
]
views.py
def ticket_detail(request, pk):
ticket=get_object_or_404(Ticket,pk=pk)
return render(request,'mmrapp/ticket_detail.html',{'ticket':ticket})
html
{% extends 'mmrapp/__l_single_column.html' %}
{% load static %}
{% block main_col %}
<div class="ticket">
<h2>Ticket: {{ticket.pk}}</h2>
</div>
{% endblock main_col %}
As I know, <int:pk>
syntax is available only through path
function (introduced in Django 2.0), not url
, so you should use path
function or refuse of using this syntax and switch to the old regex one:
url(r'ticket/(?P<pk>\d+)/', views.ticket_detail, name='ticket_detail')
Also you need to cast pk
parameter in your view to int
as all parameters captured with regex are strings.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments