【问题标题】:django.urls.exceptions.NoReverseMatch: Reverse for 'favorite' not founddjango.urls.exceptions.NoReverseMatch:找不到“最喜欢”的反向
【发布时间】:2018-12-03 10:37:27
【问题描述】:

Django 2.0 版

views.py

from django.views import generic
from .models import Album


class IndexView(generic.ListView):
    template_name = 'music/index.html'
    context_object_name = 'all_albums'

    def get_queryset(self):
        return Album.objects.all()

class DetailView(generic.DetailView):
    model = Album
    template_name = 'music/details.html'

details.html

{% extends 'music/base.html' %}

{% block body %}

    <!-- {{album}} -->

    <img src="{{album.album_logo}}">

    <h1>{{album.album_title}}</h1>
    <h3>{{album.artist}}</h3>



    {% if error_message %}
        <p><strong>{{ error_message }}</strong></p>
    {% endif %}    

    <form action="{% url 'music:favorite' album.id %}" method="post">
        {% csrf_token %}
        {% for song in album.song_set.all %}
            <input type="radio" id="song{{ forloop.counter }}" name="song" value="{{ song.id }}">
            <label for="song{{ forloop.counter }}">
                {{ song.song_title }}
                {% if song.is_favorite %}
                    <img src="http://i.imgur.com/b9b13Rd.png" />
                {% endif %}
            </label><br> 
        {% endfor %}
        <input type="submit"  value="Favorite">

    </form>
{% endblock %}

index.html

{% extends 'music/base.html' %}

{% block body %}
    {% if all_albums %}
        <h3>Here are all my Albums:</h3>
        <ul>


            {% for album in all_albums %}


            <li><a href="{% url 'music:details' album.id %}">{{ album.album_title }}</a></li>

            {% endfor %}
        </ul>
    {% else %}
        <h3>You don't have any albums</h3>
    {% endif %}         
{% endblock %}

base.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Viberr</title>
    {% load staticfiles %} 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"  />
    <link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type="text/css">
    <link rel="stylesheet" type="text/css" href="{% static 'music/style.css' %}" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>
    <nav class="navbar navbar-inverse">
                <div class="container-fluid">

                    <!-- Header -->
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#topNavBar">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" href="{% url 'music:index' %}">University of Calicut</a>
                    </div>

                    <!-- Items -->
                    <div class="collaps navbar-collaps" id="topNavBar">
                        <ul class="nav navbar-nav">
                            <li class="active">
                                <a href="{% url 'music:index' %}">
                                    <span class="glyphicon glyphicon-cd" aria-hidden="true"></span>&nbsp;
                                    Albums
                                </a>
                            </li>
                            <li class="">
                                    <a href="{% url 'music:index' %}">
                                        <span class="glyphicon glyphicon-music" aria-hidden="true"></span>&nbsp;
                                        Songs
                                    </a>
                            </li>
                        </ul>

                        <form class="navbar-form navbar-left" role="search" method="GET" action="#">
                            <div>
                                <input type="text" class="form-control" name="q" value="">
                                <button type="submit" class="btn btn-default">Search</button>
                            </div>

                        </form>

                        <ul class="nav navbar-nav navbar-right">
                            <li class="">
                                <a href="#">
                                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>&nbsp;
                                    Add Album
                                </a>
                            </li>

                            <li class="">
                                    <a href="#">
                                        <span class="glyphicon glyphicon-off" aria-hidden="true"></span>&nbsp;
                                        Logout
                                    </a>
                            </li>

                        </ul>


                    </div>


                </div>
    </nav>
{% block body %}

{% endblock %}

</body>
</html>

urls.py(音乐)

from django.conf.urls import url
from .import views
from django.urls import path


app_name = 'music'

urlpatterns = [


    url(r'^$',views.IndexView.as_view(), name='index'),

    url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='details'),

]

models.py

from django.db import models



class Album(models.Model):
    artist = models.CharField(max_length=250)
    album_title=models.CharField(max_length=500)
    genre=models.CharField(max_length=100)
    album_logo=models.CharField(max_length=1000)


    def __str__(self):
        return self.album_title + ' - ' + self.artist

class Song(models.Model):
    album = models.ForeignKey(Album, on_delete=models.CASCADE)
    file_type = models.CharField(max_length=10)
    song_title = models.CharField(max_length=250) 
    is_favorite = models.BooleanField(default=False)


    def __str__(self):
        return self.song_title
  1. 我附上了我的主页和链接页面的图片, 请帮我解决这个问题

n上面的代码,第二页给我报错了

django.urls.exceptions.NoReverseMatch:找不到“最喜欢的”的反向。 'favorite' 不是有效的视图函数或模式名称**

请有人帮我找出我错的地方!提前致谢!

【问题讨论】:

    标签: python django django-models django-templates frameworks


    【解决方案1】:

    我在这里看不到任何名称为 favorite 的 url,这也是错误告诉你的:

    app_name = 'music'
    
    urlpatterns = [
    
    
        url(r'^$',views.IndexView.as_view(), name='index'),
    
        url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='details'),
    
    ]
    

    您可能复制粘贴了 details.html,或者您只是忘记在应用“音乐”中指定“收藏”网址

    编辑: 正如您已经澄清的那样,您无法在 onw 上解决问题: 像这样更改 details.html:

    <form action="{% url 'music:favorite' album.id %}" method="post">
    

    替换为

    <form action="#" method="post">
    

    你的网站至少应该加载。

    EDIT2:

    观看您在评论中链接的 youtube 视频后: 所有答案都在视频及其 cmets 中! 您没有更新 details.html。密切关注 min 1:50。 顺便说一句,我替换表单操作的解决方案也可以,但输出会与您的预期略有不同...... 我希望这能让你开始;)

    【讨论】:

    • youtube.com/…我根据本教程制作了这个webapp,没有名为收藏夹的url。其实我被困住了。请帮帮我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多