【发布时间】: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<slug>[-\w]+)/edit/$', views.edit_Profile, name='edit_Profile'), -
好眼光!但不幸的是,模板问题仍然存在。