在/ project / users / 1 / stories / 1 /
处的NoReverseMatch找不到带有参数'('',)'的'user'的反向。尝试了1个模式:['项目/用户/(?P [0-9] +)/ $']
有人知道为什么当我按“ python manage.py runserver”时为什么会遇到此错误吗?它曾经可以正常工作,现在却不能。我已经看到问题可能出在user_id或user.id,但是我看不到它!这是我的代码:
project / 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)
project / 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'),
]
project / templates / project / 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 %}
答案 0 :(得分:0)
在您的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
)。
现在在模板中编写:
{% url 'project:user' story.author.id %}
,但是由于story
是str
,因此它没有.author
属性,因此它将被评估为string_if_invalid
,除非您另外指定,否则空字符串''
。
因此,您需要将上下文中的故事本身传递给:
context = {
'user_id': user_id,
'story_id': story_id,
'title': story.title,
'story': story,
'likes': story.likes,
'comments': story.comments
}