CreateView的NoReverseMatch

时间:2016-08-05 16:57:48

标签: python django url

我正在使用带有模板的CreateView(CBV),该模板具有审查律师的表格。

当我尝试在模板中使用URL路由时,出现此错误:

Exception Value: Reverse for 'lawyerreview_create' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['fathers/lawyers/(?P<lawyerreview_slug>[\\w-]+)/createreview/$']

我想反转此网址,例如:父亲/律师/ lawyer-name / createreview。但经过两天尝试不同的事情,我并没有更接近这个目标。有人能告诉我我做错了什么吗?我不只是在寻找一个答案,我谦卑地要求有人ELI5在django中正确链接。

模型:

class Lawyer(models.Model):
name = models.CharField(max_length=100, default='')
lawyer_slug = models.SlugField(default='')

def __str__(self):
    return self.name


class Review(models.Model):
    lawyer = models.ForeignKey(Lawyer, null=True)
    review_title = models.CharField(max_length=69, default='')
    review_created = models.DateTimeField('Date of Review', auto_now_add=True)
    user_name = models.CharField(max_length=100)
    rating = models.IntegerField()
    review_comment = models.TextField(default='')
    review_slug = models.SlugField(default='')

views.py

from django.views.generic import ListView, DetailView
from django.views.generic import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy

from .models import Lawyer, Review

class LawyerDetail(DetailView):
    model = Lawyer

    template = 'lawyer_detail.html'

    context_object_name = 'lawyers'

    def get_object(self):
        lawyer_slug = Lawyer.objects.get(
            lawyer_slug=self.kwargs.get('lawyer_slug')
        )
        return lawyer_slug

    def get_context_data(self, **kwargs):
        context = super(LawyerDetail, self).get_context_data(**kwargs)
        context['lawyer_review'] = self.object.review_set.all()
        return context


class LawyerReviewCreate(CreateView):
    model = Review
    fields = ['lawyer', 'rating', 'dos', 'review_comment']

class LawyerReviewUpdate(UpdateView):
    model = Review
    fields = ['lawyer', 'rating', 'dos', 'review_comment']

class LawyerReviewDelete(DeleteView):
    model = Review
    success_url = reverse_lazy('lawyer_detail')

template.html(生成NoReverseMatch错误的部分)

{% if lawyer_review %}
    {% for review in lawyer_review %}
    <div class="review_buttom_wrapper">
        <button class="review_button" href="{% url 'lawyerreview_create' lawyer.lawyerreview_create %}">
            <strong>Review</strong> {{ review.lawyer.name }}
        </button>
    </div>

urls.py

url(r'^lawyers/(?P<lawyer_slug>[\w-]+)/$', LawyerDetail.as_view(), name='lawyer_detail'),
    url(r'^lawyers/(?P<lawyer_slug>[\w-]+)/createreview/$', LawyerReviewCreate.as_view(), name='lawyer_createreview'),
    url(r'^lawyers/(?P<lawyer_slug>[\w-]+)/update/$', LawyerReviewUpdate.as_view(), name='lawyer_editreview'),
    url(r'^lawyers/(?P<lawyer_slug>[\w-]+)/delete/$', LawyerReviewDelete.as_view(), name='lawyer_deletereview'),  

使用wobbily_col解决方案后生成错误:

Exception Value: Reverse for 'lawyerreview_create' with arguments '()' and keyword arguments '{'lawyerreview_slug': ''}' not found. 0 pattern(s) tried: []

将模板更改为:

<button class="review_button" href="{% url 'lawyerreview_create' review.lawyer.lawyer_slug %}">

立即收到此错误:

Exception Value: Reverse for 'lawyerreview_create' with arguments '('michael-ferrin',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

律师的名字现在显示在网址中。

编辑模板:

<a href="{% url 'lawyer_createreview' review.review_slug %}">
    <button class="review_button">
        <strong>Review</strong> {{ review.lawyer.name }}
    </button>
</a>

现在可以正确链接到review_form模板。但是在提交时,我收到了这个错误:

Exception Value: Reverse for 'lawyer_createreview' with arguments '()' and keyword arguments '{'review_slug': ''}' not found. 1 pattern(s) tried: ['fathers/lawyers/(?P<lawyer_slug>[\\w-]+)/createreview/$']

我原本希望被转回到将我发送到评论表单的页面。

1 个答案:

答案 0 :(得分:1)

url标记将反向名称和参数/关键字参数作为参数:

url(r'^lawyers/(?P<lawyerreview_slug>[\w-]+)/createreview/$', LawyerReviewCreate.as_view(), name='lawyerreview_create'),

所以在这种情况下,它需要你的网址名称(lawyerview_create)和lawyerreview_slug作为关键字参数。你传递的是一个似乎不存在的位置参数(lawyer.lawyerreview_create)。

像这样传递lawyer_slug:

    <button class="review_button" href="{% url 'lawyerreview_create' lawyerreview_slug=lawyer_review.lawyer.lawyer_slug %}">