我正在努力让用户可以编辑他们创建的文章。我觉得我很接近,但不能完全弥合我所做的和我需要做的事情的差距。
文章详细信息页面上的模板:
{% if request.user == article.author %}
<p>
<a href="{% url 'articles:edit_article' %}"
Edit article
</a>
</p>
{% endif %}
这似乎工作正常。
urls.py:
url(r'^(?P<slug>[\w-]+)/$', views.article_detail, name="detail"),
url(r'^(?P<slug>[\w-]+)/edit/$', views.edit_article, name="edit_article"),
views.py:
def edit_article(request):
if request.method == 'POST':
form = forms.EditArticle(request.POST, slug=request.slug)
if form.is_valid():
form.save()
return redirect('articles:list')
else:
form = forms.EditArticle(slug=request.slug)
args = {'form': form}
return render(request, 'articles/edit_article.html', args)
forms.py
class EditArticle(forms.ModelForm):
class Meta:
model = models.Article
fields = (
'title',
'body',
'slug',
'thumb'
)
并且edit_article.html:
{% extends 'base_layout.html' %}
{% block content %}
<h1>Edit Article {{ article.title }}</h1>
<div class="profile">
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Update Profile</button>
</form>
</div>
{% endblock %}
这是追溯:
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\...\...\...\articles\views.py" in article_detail
18. return render(request, "articles/article_detail.html", {'article':article})
File "C:\Python27\lib\site-packages\django\shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Python27\lib\site-packages\django\template\loader.py" in render_to_string
68. return template.render(context, request)
File "C:\Python27\lib\site-packages\django\template\backends\django.py" in render
66. return self.template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
207. return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
990. bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render_annotated
957. return self.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
177. return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
990. bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render_annotated
957. return self.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
72. result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
990. bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render_annotated
957. return self.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
322. return nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
990. bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render_annotated
957. return self.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
458. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Python27\lib\site-packages\django\urls\base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Python27\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
497. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /articles/testing-1212/
Exception Value: Reverse for 'edit_article' with no arguments not found. 1 pattern(s) tried: [u'articles/(?P<slug>[\\w-]+)/edit/$']
我猜这个问题是在slug的视图中。我试图以我的工作视图为基础来编辑用户配置文件,所以我猜我在翻译中丢失了一些东西。我觉得这只是工作中的一两个调整。有什么想法吗?
答案 0 :(得分:1)
你应该:
{% if request.user == article.author %}
<p>
<a href="{% url 'articles:edit_article' article.slug %}"
Edit article
</a>
</p>
{% endif %}
您的观点
def edit_article(request,slug):
article = Article.objects.get(slug=slug) # Better to use get_object_or_404 from django.http
if request.method == 'POST':
form = forms.EditArticle(request.POST, instance=article)
if form.is_valid():
form.save()
return redirect('articles:list')
else:
form = forms.EditArticle(instance=article)
args = {'form': form}
return render(request, 'articles/edit_article.html', args)