加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Django表单的NoReverseMatch

发布时间:2020-12-20 11:28:07 所属栏目:Python 来源:网络整理
导读:我是Django的新手,我遇到了NoReverseMatch错误.有谁知道我怎么解决这个问题? 异常值:反向’profile_list.html’,参数'()’和关键字参数'{}’未找到. edit_profile.html h1Add Profile/h1form action="{% url 'questions-new' %}" method="POST" {% csrf_to
我是Django的新手,我遇到了NoReverseMatch错误.有谁知道我怎么解决这个问题?

异常值:反向’profile_list.html’,参数'()’和关键字参数'{}’未找到.

edit_profile.html

<h1>Add Profile</h1>

<form action="{% url 'questions-new' %}" method="POST">
  {% csrf_token %}
  <ul>
    {{ form.as_ul }}
  </ul>
  <input type="submit" value="Save" />
</form>

<a href="{% url 'profile-list' %}">back to list</a>

urls.py

from django.conf.urls import patterns,include,url

import questions.views

urlpatterns = patterns('',url(r'^$',questions.views.ListProfileView.as_view(),name='profile-list'),url(r'^new$',questions.views.CreateProfileView.as_view(),name='questions-new',),)

views.py

from django.views.generic import ListView

from questions.models import Profile

from django.core.urlresolvers import reverse
from django.views.generic import CreateView

class ListProfileView(ListView):
    model = Profile
    template_name = 'profile_list.html'

class CreateProfileView(CreateView):

    model = Profile
    template_name = 'edit_profile.html'

    def get_success_url(self):
        return reverse('profile_list.html')

解决方法

你的get_success_url错了.将其更改为以下内容:

def get_success_url(self):
    return reverse('profile-list')

reverse应与您在urls.py模式中提供的名称一起使用,而不是模板名称.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读