python - 未找到带有参数 '('',)' 的 'user' 的反向操作。尝试了 1 种模式:['project/users/(?P[0-9]+)/$']
问题描述
/project/users/1/stories/1/ 处的 NoReverseMatch
未找到带有参数 '('',)' 的 'user' 的反向操作。尝试了 1 种模式:['project/users/(?P[0-9]+)/$']
有谁知道为什么我在按下“python manage.py runserver”时会遇到这个错误?它以前工作得很好,现在却不行了。我已经看到问题可能出在 user_id 或 user.id 上,但我真的看不出来!这是我的代码:
项目/views.py
def story(request, user_id, story_id):
if story_id is not None:
story = get_object_or_404(Story, pk=story_id)
else:
story = Story()
story.user_id = user_id
if request.method == 'POST':
story.title = request.POST['title']
story.story = request.POST['story']
story.date = timezone.now()
story.save()
return HttpResponseRedirect(reverse('project:story', args=(user_id,)))
else:
context = {
'user_id': user_id,
'story_id': story_id,
'title': story.title,
'story': story.story,
'likes': story.likes,
'comments': story.comments
}
return render(request, 'project/story.html', context)
项目/urls.py
urlpatterns = [
path('', views.index, name='index'),
path('register/<int:user_id>/', views.register, name='register'),
path('login/<int:user_id>/', views.login, name='login'),
path('users/<int:user_id>/', views.user, name='user'),
path('users/<int:user_id>/stories/<int:story_id>/', views.story, name='story'),
]
项目/模板/项目/story.html
{% extends "project/base.html" %}
{% block content %}
{% if story_id %}
<div class="post-preview">
<h2 class="post-title"> {{ story.title }}</h2>
<p class="post-subtitle">
{{ story.story }}
</p>
<p class="post-meta">Posted by
<a href="{% url 'project:user' story.author.id %}">{{ story.author.username }}</a>
on {{ story.date }}
<i class="fas fa-thumbs-up"> {{ story.likes }}</i>
<i class="fas fa-comment"> {{ story.comments }}</i>
</p>
</div>
<div class="post-preview">
<h2> Comments </h2>
{% for com in latest_comments %}
<div class="post-preview">
<p class="post-subtitle"> {{ comment.com }}</p>
</div>
{% endfor %}
</div>
<div class="post-preview">
<form action="{% url 'project:story' user.id story.id %}" method="post">
{% csrf_token %}
<div class="form-group">
<label for="text">Comment</label>
<textarea id="text" name="text"
class="form-control" placeholder="Comment" rows="4">{{ comment.com }}
</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
{% else %}
{% if request.story.author.id == user.id %}
<form action="{% url 'project:story' user.id story.id %}" method="post">
{% csrf_token %}
<div class="form-group">
<label for="title">Title</label>
<input type="text" id="title" name="title"
class="form-control" placeholder="Title" value="{{ story.title }}"/>
</div>
<div class="form-group">
<label for="text">Story</label>
<textarea id="text" name="text"
class="form-control" placeholder="Story" rows="10">{{ story.story }}
</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endif %}
{% endif %}
{% endblock %}
解决方案
在你context
我们看到:
context = {
'user_id': user_id,
'story_id': story_id,
'title': story.title,
'story': story.story,
'likes': story.likes,
'comments': story.comments
}
所以这个story
变量不包含一个Story
对象,它包含它的story
属性(可能,基于视图的其余部分,一个str
ing)。
现在在您的模板中编写:
{% url 'project:user' story.author.id %}
但由于story
是一个str
ing,它没有.author
属性,所以string_if_invalid
除非你另外指定,否则它将被评估为空字符串''
。
因此,您需要将故事本身在您的上下文中传递给:
context = {
'user_id': user_id,
'story_id': story_id,
'title': story.title,
'story': story,
'likes': story.likes,
'comments': story.comments
}
推荐阅读
- excel - 如果满足条件,则合并单元格中的数据,然后删除重复的行
- swift - 如何允许用户只获取他们自己的数据?迅速
- azure - 提供对多个存储帐户的 Azure VM 访问
- amazon-web-services - 同时担任多个 IAM 角色如何工作?
- algorithm - FFMPEG色彩增强算法
- go - 创建通用渠道
- sql - snowflake-sql:min vs first_value windows函数的情况
- angular - 验证码会话空
- grafana - 如何将 Grafana 时间序列 - 轴值的软最大值分配给变量并在另一个仪表板面板中使用它?
- asynchronous - ReadAsync 只阻塞一次(一次读取),然后它不会在循环中再次阻塞