【问题标题】:Django - NoReverseMatch at /Profile/userDjango - 在 /Profile/user 的 NoReverseMatch
【发布时间】:2017-02-28 19:39:00
【问题描述】:

我的问题是单击主页上的某个链接后出现 NoReverseMatch 错误。问题指向我拒绝删除的文本文件。

这是导致问题的代码

<a href="{% url 'edit_Profile' slug=Profile.slug %}">Edit me!</>

这是我的代码

views.py

from django.shortcuts import render, redirect
from django.views.generic.base import TemplateView
from webapp.models import Profile
from webapp.forms import ProfileForm

def index(request):
    Profiles = Profile.objects.all()
    return render(request, 'index.html', {
    'Profiles': Profiles,
})

def Profile_detail(request, slug):

    Profiles = Profile.objects.get(slug=slug)
    return render(request, 'Profiles/Profile_detail.html', {
    'Profile': Profile,
})



def edit_Profile(request, slug):

    Profile = Profile.objects.get(slug=slug)
    form_class = ProfileForm

    if request.method == 'POST':

        form = form_class(data=request.POST, instance=Profile)
        if form.is_valid():

            form.save()
            return redirect('Profile_detail', slug=Profile.slug)

    else:
        form = form_class(instance=Profile)

    # and render the template
    return render(request, 'Profiles/edit_Profile.html', {
        'Profile': Profile,
        'form': form,
    })

url.py

from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView

from webapp import views


from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView

from webapp import views


urlpatterns = [
    url(r'^$', views.index, name='home'),
    url(r'^about/$',
        TemplateView.as_view(template_name='about.html'), name='about'),
    url(r'^contact/$',
        TemplateView.as_view(template_name='contact.html'), name='contact'),
    url(r'^Profiles/(?P<slug>[-\w]+)/$', views.Profile_detail, 
        name='Profile_detail'),
    url(r'^things/(?P<slug>[-\w]+)/edit/$', 
        views.edit_Profile,
        name='edit_Profile'),
]

forms.py

from django.forms import ModelForm

from webapp.models import Profile

class ProfileForm(ModelForm):
    class Meta:
        model = Profile
        fields = ('name', 'description',)

profile_detail.html

{% extends 'base.html' %}
{% block title %}
     {{ Profile.name }} - {{ block.super }}
{% endblock title %}

{% block content %}
    <h1>{{ Profile.name }}</h1>
    <p>{{ Profile.description }}</p>
    <a href="{% url 'edit_Profile' slug=Profile.slug %}">Edit me!</>
    {% endblock content %}

【问题讨论】:

  • 我没看到你的网址edit_Profile是在urlpatterns中配置的,你错过了url(r'^profile/(?P&lt;slug&gt;[-\w]+)/edit/$', views.edit_Profile, name='edit_Profile'),
  • 好眼光!但不幸的是,模板问题仍然存在。

标签: python django slug


【解决方案1】:
url(r'^Profiles/(?P<slug>[-\w]+)/$', views.Profile_detail, 
        name='Profile_detail')

您的模板中的名称是 Profile_detail 而不是 edit_Profile

例如使用命名的url,你应该在url.py

(r'^about_me/', about_me_view, name='about_me')

在模板中:

<a href={% url 'about_me' %}>about me</a>

【讨论】:

    【解决方案2】:

    url 模板标签中的第一个参数与 urls.py 中的名称匹配,而不是与视图函数的名称匹配。 另一件事是您没有指向 edit_Profile 函数的条目。

    要完成这项工作,您需要在 urls.py 中添加一个指向函数的 urlpattern,名称为“edit_Profile”,例如:

    url(r'^Profiles/(?P<slug>[-\w]+)/edit$', views.edit_Profile, 
        name='edit_Profile')
    

    推荐阅读:https://docs.djangoproject.com/en/1.10/topics/http/urls/

    【讨论】:

    猜你喜欢
    • 2022-06-14
    • 1970-01-01
    • 2022-07-04
    • 2016-07-20
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    相关资源
    最近更新 更多