没有Part匹配给定的查询

时间:2018-06-05 03:35:13

标签: python django

我在django打招呼。我有错误的问题:

  

没有Part匹配给定的查询。

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=None, 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

]

还有一点 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 %}

在Models.py中我有一个类Part(models.Model)

请帮我找错。

2 个答案:

答案 0 :(得分:1)

错误正在引发,因为您正在尝试get您的数据库中没有的Part实例。也就是说,您通过网址提供pk值,而且它是与DB不匹配。
您只需查询现有的Part对象即可避免此错误。或者,您可以修改如下的响应,

from django.http.response import HttpResponse 
def PartyNumView(request, pk=None, 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)
    try:
        onePart = Part.objects.get(pk=pk)
    except Part.DoesNotExist:
        return HttpResponse("Part instance not found with pk-{}".format(pk))
    return render_to_response('part_list.html', {'PartyNum': context, 'onePart': onePart})


UPDATE-1
更新您的urls.py,如下所示

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'parties/(?P<pk>[\d]+)/(?P<page_number>[\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

]

答案 1 :(得分:0)

所以你有3个网址指向同一个视图。

url(r'^parties/(\d+)/$', PartyNumView),
url(r'^parties', PartyNumView),
url(r'^parties/(?P<pk>[\d]+)$', PartyNumView, name='onePart'),

您列出的第一个网址优先,但您不接受该网址中的PK,而您的功能参数列出pk=None。所以这个表达不会起作用:

onePart = get_object_or_404(Part, pk = pk)

尝试删除前两个网址并保留最后一个网址,因为它接受PK。另外,在您的函数参数中,将pk=None更改为pk

另外,请查看您列出的最后两个网址。两者都指向相同的视图,因此您应该决定应该保留哪一个。