Django:带有PK问题的DetailView URL

时间:2018-07-18 01:21:04

标签: python regex django webapp2 kwargs

我是Django的新手,我正在尝试构建教堂目录应用程序来管理教堂(群)中的用户(绵羊)。在为名为ChurchMembers的模型添加CBV-DetailView之前,一切似乎都正常。我想使用ChurchMember的pk来创建一个DetailView以便该会员拥有个人资料。

我在Debug中收到以下错误消息:

NoReverseMatch at /flock/members/
Reverse for 'view_member_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['flock/members/(?P<member_pk>\\d+)/$']

这是我的views.py(仅显示有问题的DetailView):

class view_member_detail(DetailView):
model = models.ChurchMember
template_name = 'flock_app/member_profile.html'
context_object_name = 'member_detail'
pk_url_kwarg = 'member_pk'

我预感这与我的urls.py文件的设置方式有关。可能是不正确的正则表达式,或者顺序可能不正确。 urls.py如下:

app_name = 'flock_app'

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^register/$', views.register, name='register'),
    url(r'^login/$', views.user_login, name='user_login'),
    url(r'^logout/$', views.user_logout, name='user_logout'),
    url(r'^add_church/$', views.add_church, name='add_church'),
    url(r'^add_member/$', views.add_member, name='add_member'),
    url(r'^members/$', views.view_members_list.as_view(), name='view_members_list'),
    url(r'^members/(?P<member_pk>\d+)/$', views.view_member_detail.as_view(), name='view_member_detail'),
    url(r'^churches2/$', views.view_churches_list.as_view(), name='view_churches_list'),
    url(r'^(?P<pk>[-\w]+)/$', views.view_churches_detail.as_view(), name='view_churches_detail'),
    url(r'^update/(?P<pk>\d+)/$', views.update_church_detail.as_view(), name='update_church_detail'),
    url(r'^delete/(?P<pk>[-\w]+)/$', views.delete_church.as_view(), name='delete_church'),
    url(r'^home', views.home, name='home')
]

这是我从HTML调用的DetailView:

  <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
    <a class="dropdown-item" href="{% url 'flock_app:view_member_detail' pk=mem.member_pk %}">Sheep Profile</a>
    <a class="dropdown-item" href="#">Delete sheep</a>
  <div/>

任何帮助将不胜感激!

编辑-已添加HTML的源代码

{% extends 'flock_app/base.html' %}
{% load crispy_forms_tags %}

{%  block body_block%}

<div class="jumbotron">
  {% if members %}

  <h1 class="display-3 text-center">All Sheep in Flocks <button type="button" class="btn btn-outline-secondary" onclick="location.href='{% url 'flock_app:add_member' %}'">Add Sheep</button></h1>
  <table class="table table-hover">
    <thead class=>
      <tr>
        <th scope="col"></th>
        <th scope="col">Flock</th>
        <th scope="col">First Name</th>
        <th scope="col">Last Name</th>
        <th scope="col">Email</th>
        <th scope="col">Phone</th>
        <th></th>
      </tr>
    </thead>
    {% for mem in members %}
    <tbody>
      <tr>
        <th scope="row"></th>
        <td>{{ mem.Flock }}</td>
        <td>{{ mem.First_Name }}</td>
        <td>{{ mem.Last_Name }}</td>
        <td>{{ mem.Email }}</td>
        <td>{{ mem.Phone_Number }}</td>
        <td>
          <div class="dropdown">
            <a class="btn btn-outline-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Manage</a></th>
          <div/>
          <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
            <a class="dropdown-item" href="{% url 'flock_app:view_member_detail' pk=mem.member_pk %}">Sheep Profile</a>
            <a class="dropdown-item" href="#">Delete sheep</a>
          <div/>
        </td>
      </tr>
      {% endfor %}
      <table/>
  {% else %}

    <h1 class="text-center">No Sheep Found</h1>
    <h1 class="lead text-center">Click <a href="{% url 'flock_app:add_member' %}">here</a> to add Sheep to <em>His</em>_Flock</h2>

  {% endif %}


</div>
{% endblock %}

编辑2-DJANGO错误代码

NoReverseMatch at /flock/members/
Reverse for 'view_member_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['flock/members/(?P<member_pk>\\d+)/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/flock/members/
Django Version: 1.11.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'view_member_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['flock/members/(?P<member_pk>\\d+)/$']
Exception Location: C:\Program Files\Python35\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable:  C:\Program Files\Python35\python.exe
Python Version: 3.5.4
Python Path:    
['C:\\Users\\gabrielcastillo\\Google Drive\\git_base\\flock',
 'C:\\Program Files\\Python35\\python35.zip',
 'C:\\Program Files\\Python35\\DLLs',
 'C:\\Program Files\\Python35\\lib',
 'C:\\Program Files\\Python35',
 'C:\\Program Files\\Python35\\lib\\site-packages',
 'C:\\Program Files\\Python35\\lib\\site-packages\\win32',
 'C:\\Program Files\\Python35\\lib\\site-packages\\win32\\lib',
 'C:\\Program Files\\Python35\\lib\\site-packages\\Pythonwin']
Server time:    Wed, 18 Jul 2018 02:21:37 +0000
Error during template rendering
In template C:\Users\gabrielcastillo\Google Drive\git_base\flock\templates\flock_app\base.html, error at line 0

Reverse for 'view_member_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['flock/members/(?P<member_pk>\\d+)/$']

2 个答案:

答案 0 :(得分:0)

网址从

更改
url(r'^members/(?P<member_pk>\d+)/$', views.view_member_detail.as_view(), name='view_member_detail'),

url(r'^members/(?P<pk>\d+)/$', views.view_member_detail.as_view(), name='view_member_detail'),

答案 1 :(得分:0)

我已经解决了这个问题!好像我在HTML页面中调用了另一个View,这引发了错误。一旦清除,我就可以访问所有带有相应确定信息的页面!