渲染时捕获NoReverseMatch:反向'accounts_profile_view',参数'('',)'和关键字参数'{}'未找到

时间:2012-02-09 14:31:11

标签: django django-templates django-views django-urls

正如你所看到的那样,我在渲染时有一个奇怪的Caught NoReverseMatch:反向'accounts_profile_view',参数'('',)'和关键字参数'{}'找不到。

最奇怪的是在django中使用shell_plus,导入用户配置文件,然后从django.urlcore.resolvers导入反向函数我这样做:

reverse('accounts_profile_views', args=[username])

它运作得很好!

我为你们创造了一堆要点,帮助我审查它们,这里是它们。

这是模型 https://gist.github.com/140127f6303660baf8d9

这是我的观点文件 https://gist.github.com/1a3b9651eef3fb9d22db

这是我的模特网址 https://gist.github.com/66fe6b1f068739f940db

这是整个项目网址 https://gist.github.com/edea781b1efca341230b

当然还有错误 https://gist.github.com/8b84bd0394d4b047dce8

感谢您的帮助社区!

======更新======

我发现了一些东西,在contact_us视图和模板上发生了一些奇怪的事情,当我转到contact-us页面时,标题上的request.user.username显示一个空的var。

继承我的contact_us url,它是项目主要视图的一部分:

from django.conf.urls.defaults import * 
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('apps.maincoke.views',
    url(r'^(?i)$', 'home', name='home'),
    url(r'^(?i)faqs/$', direct_to_template, {'template': 'maincoke/faqs.html'}, name="main_faqs"),
    url(r'^(?i)contact-us/$','contact_us', name='contact_us'),
)

这是contact_us视图:

@login_required
@render_to("maincoke/contact_us.html")
def contact_us(request):
    mail_status = None

    if request.method == 'POST':
        contact_us_form = ContactUsForm(request, request.POST)
        if contact_us_form.is_valid():
            mail_status = contact_us_form.send_as_mail()
            if mail_status:
                 contact_us_form = ContactUsForm(request)
    else:
        contact_us_form = ContactUsForm(request)
    return { 'contact_us_form' : contact_us_form, 'mail_status' : mail_status }

这是表格,我使用django-uni-form

class ContactUsForm(forms.Form):
    name = forms.CharField(label='Nombre', max_length=120, required=True)
    email = forms.CharField(max_length=200, required=True)
    message = forms.CharField(label='Mensaje', widget=forms.Textarea(attrs={'cols':'40'}), 
    required=True)

    MAX_MESSAGE_LENGTH = 2000
    # uniForm Helper
    helper = FormHelper()
    helper.form_id = "contact_us_form"
    layout = Layout(
        Fieldset('',
            'name', 'email', 'message'
        )
    )
    helper.add_layout(layout)

    # Submit button(s)
    submit = Submit('submit','Enviar')
    helper.add_input(submit)


    class Meta:
        fields = ['name', 'email', 'message']

    def __init__(self, request, *args, **kw):
        super(ContactUsForm, self).__init__(*args, **kw)
        self.request = request

    def clean_message(self):
        message_data = self.cleaned_data['message']
        if len(message_data) > self.MAX_MESSAGE_LENGTH:
            raise forms.ValidationError("Mensaje demasiado largo")

    def send_as_mail(self):
        data = self.cleaned_data
        send_status=True
        mail_content_html = render_to_string('mails/contactmail.html', {'data':data})
        mail_content_text = strip_tags(mail_content_html)
        mail = EmailMultiAlternatives("Mensaje enviado de %s" % data['name'], 
                                 mail_content_text, 
                                 settings.DEFAULT_FROM_EMAIL, 
                                 settings.CONTACT_MAIL_RECIPIENT
                                )
        mail.attach_alternative(mail_content_html, "text/html")
        try:
             mail.send(fail_silently=False)
        except:
             send_status=False

        return send_status

模板的信息根本没有呈现!

2 个答案:

答案 0 :(得分:1)

错误显示错误:username参数正在评估空字符串。你需要找出原因。

答案 1 :(得分:0)

from django.conf.urls.defaults import *

urlpatterns = patterns('apps.accounts.views',
url(r'^(?P<username>\w+)/$', 'profile_view', {}, name="accounts_profile_view"),
url(r'^(?P<username>\w+)/edit/$', 'profile_edit', {}, name="accounts_profile_edit"),
)

当此处显示的数据:r'^(?P username \w+)与反向def中的给定数据不匹配时发生:反向('accounts_profile_views',args = [用户名])

\w  Match a word character (alphanumeric plus "_")

调试视图并查看此用户名中的内容,如果它包含空格,点或空字符串,因为我们的PyMASTER @Daniel Roseman说...这不会匹配并会引发此异常。