Django - / Profile / user的NoReverseMatch

时间:2017-02-28 19:39:00

标签: python django slug

我的问题是,点击主页上的其中一个链接后,我收到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 %}

2 个答案:

答案 0 :(得分:0)

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

模板中的名称Profile_detail不是edit_Profile

例如,使用已命名的网址,您应该在url.py中执行:

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

在模板中:

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

答案 1 :(得分:0)

url templatetag中的第一个参数与urls.py中的名称匹配,而不是与视图函数的名称相对应。 另一件事是你没有指向edit_Profile函数的条目。

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

/*Place where i should change something*/
int temp = a[i][i];
a[i][i] = a[i][c];
a[i][c] = temp;

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