Reputation: 313
May be it's stupid question, but I'm begginer in django. I have a problem with error:
"PartyNumView() missing 1 required positional argument: 'pk'"
views.py:
from description.models import Part
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render_to_response, get_object_or_404
def PartyNumView(request, pk, page_number = 1):
all_parties = Part.objects.all()
current_page = Paginator(all_parties, 10)
try:
context = current_page.page(page_number)
except PageNotAnInteger:
context = current_page.page(1)
except EmptyPage:
context = current_page.page(current_page.num_pages)
onePart = get_object_or_404(Part, pk = pk)
return render_to_response('part_list.html', {'PartyNum': context, 'onePart': onePart})
urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^parties/(\d+)/$', PartyNumView),
url(r'^parties', PartyNumView),
url(r'parties/(?P<pk>[\d]+)$', PartyNumView, name='onePart'),
url(r'^main/', TemplateView.as_view(template_name='main.html')), #static html
url(r'^measures/', TemplateView.as_view(template_name='IcDesc.html')), #static html
]
And a little bit of part_list.html:
{% for object in PartyNum %}
<tr>
<td>{{ forloop.counter }}</td>
<td><a href="{% url 'onePart' pk = object.pk %}"> {{ object.Party_number }}</a></td>
<td>{{ object.Film }}</td>
<td>{{ object.Thick }}</td>
<td>{{ object.Critical_temperature }}</td>
<td>{{ object.R_s }}</td>
{% endfor %}
In Models.py I have a class Part(models.Model)
Help me please find a mistake.
Upvotes: 0
Views: 1074
Reputation: 631
The problem is your url_patterns url(r'^parties', PartyNumView),
When this url your view not recive the pk argument it raise the exception
Upvotes: 1
Reputation: 8525
You have two urls patterns for PartyNumView
;
url(r'^parties', PartyNumView),
url(r'parties/(?P<pk>[\d]+)$', PartyNumView, name='onePart'),
Just delete the first, and everything will be ok, and add the ^
before the second
url(r'^parties/(?P<pk>[\d]+)$', PartyNumView, name='onePart'),
Or In case the view serves 2 different urls, don't delete your first url
, add the ^
in the second url, then add a default argument for the pk
in the view, like None
PartyNumView(request, pk=None, page_number = 1):
all_parties = Part.objects.all()
Upvotes: 2
Reputation: 16505
The url url(r'^parties', PartyNumView),
does not parse an argument pk
, but it still calls the function PartyNumView
.
You would have to use a different view function for that specific url, or use a default value:
def PartyNumView(request, pk=None, page_number = 1):
I suggest you read the python style guide to maybe adjust your code to the standard praxis. For example, function names are usually all lowercase and separated by underscores; your PartyNumView
could be renamed to party_num_view
for example.
Using this style helps other python programmers to understand your code and provide better answers.
Upvotes: 1