首页 > 解决方案 > 未找到带有参数 '('',)' 的 '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 %}

标签: pythonhtmldjango

解决方案


在你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属性(可能,基于视图的其余部分,一个string)。

现在在您的模板中编写:

{% url 'project:user' story.author.id %}

但由于story是一个string,它没有.author属性,所以string_if_invalid除非你另外指定,否则它将被评估为空字符串''

因此,您需要将故事本身在您的上下文中传递给:

context = {
    'user_id': user_id,
    'story_id': story_id,
    'title': story.title,
    'story': story,
    'likes': story.likes,
    'comments': story.comments
}

推荐阅读