URL的NoReverseMatch

问题描述 投票:0回答:1

我一直在尝试用Django创建电影调查以进行分配,而我目前正在使用该功能。我似乎无法理解为什么它无法识别我传递的URL。

我尝试删除该框架站点上Django教程中所示的硬编码URL,但这并不能使错误消失。

这里是urls.py的摘录:

urlpatterns = [
    url(r'^$', views.index, name="index"),
    path('movie=<int:movie_id>&user=<int:user_id>/', views.movie, name='movie'),
    path('ratings/', views.ratings, name='movie'),
    path('rating/<int:movie_id>/', views.rating, name='movie'),
    path('movie=<int:movie_id>&user=<int:user_id>/vote/', views.vote, name='vote'),
    path('register/',views.register, name='register'),
]

这是我的电影视图(应该显示电影和星级收音机,以供用户对电影进行评级),在其中构造了URL并将其传递给HTML:

def movie(request,movie_id,user_id):
    movie = get_object_or_404(Movie, pk=movie_id)
    voteURL = '/polls/movie=' + str(movie_id) + '&user='+str(user_id)+'/vote/'
    context = {
        'mymoviecaption':movie.Title,
        'moviePoster': 'https://image.tmdb.org/t/p/original'+tmdb.Movies(movie.TMDBID).images().get('posters')[0].get('file_path'),
        'myrange': range(10,0,-1),
        'myuserid':user_id,
        'voteurl': voteURL,
        'mymovieid':movie_id
    }
    #print(nextURL)
    translation.activate('en')
    return HttpResponse(render(request, 'movieview.html', context=context))

HTML摘录,其中称为投票视图:

<form action="{% url voteurl %}" method="post">

    {% for i in myrange %}
        <input id="star-{{i}}" type="radio" name="rating" value={{i}}>
            <label for="star-{{i}}" title="{{i}} stars">
                <i class="active fa fa-star" aria-hidden="true"></i>
            </label>
    {% endfor %}
    <input type="submit">Vote!</input>
</form>

投票视图(应该保存到数据库并重定向到下一部电影,还没有保存到数据库,因为在确定可以使用该功能之前,我不想在记录中将其弄乱:]

def vote(request, movie_id,user_id):
    try:
        nextmovie=get_object_or_404(Movie, pk=movie_id+1)
        nextURL = '/polls/movie=' + str(movie_id + 1) + '&user='+str(user_id)+'/'
    except Http404:
        nextURL = '/polls/ratings'
    try:
        myrating = int(request.POST['rating'])
        print(myrating)
    except:
        # Redisplay the question voting form.
        return render(request, '/polls/movie=' + str(movie_id + 1) + '&user='+str(user_id)+'/', {
            'error_message': "You didn't select a choice.",
        })
    return HttpResponseRedirect(nextURL)

无论我如何尝试,尽管我说的URL是在urlpatterns中定义的,但每次尝试加载第一个电影页面时,我都会在/ polls / movie = 1&user = 9 /获得NoReverseMatch。

python html django python-3.x django-rest-framework
1个回答
0
投票

这不是您在网址中提供两个pk的方式

应该是这样

path('movie/<int:movie_id>/<int:user_id>/', views.movie, name='movie'),

此外,这也不是在模板中提供url的方式,因此应该是这样的

<form action="{% url 'vote' %}" method="post">
 # {% url 'url_name' %}
  # if app_name provided {% url 'app_name:url_name' %}

并且请逐步按照教程操作

© www.soinside.com 2019 - 2024. All rights reserved.