2017-12-28 12 views
0

私のDjangoプロジェクトのbase.htmlで指定された "Add Album"へのリンクがあります。 「アルバムを追加」リンクがしかし、クリックするとコードDjangoプロジェクト:base.htmlのリンクでエラーが発生しました

<ul class="nav navbar-nav navbar-right"> 
       <li class=""> 
        <a href="{% url 'music:album-add' %}"> 
         <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>&nbsp; Add Album 
        </a> 
       </li> 

を下回っている、それはエラーになります:

ValueError at /music/album-add/ 
invalid literal for int() with base 10: 'album-add' 
Request Method: GET 
Request URL: http://127.0.0.1:8000/music/album-add/ 
Django Version: 2.0 
Exception Type: ValueError 
Exception Value:  
invalid literal for int() with base 10: 'album-add' 
Exception Location: C:\Python34\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 947 

音楽/ views.pyファイルのコードは以下の通りです

from django.views import generic 
from django.views.generic.edit import CreateView, UpdateView, DeleteView 
from .models import Album 

#=============HOME PAGE=================== 
class IndexView(generic.ListView): 
    #specify template being used 
    template_name='music/index.html' #when we get a list of all albums, plug them into this template 
    context_object_name='all_albums' #if you don't use this variable it is automatically just object_list (which is used in index.html) 

    #make a query set 
    def get_queryset(self): 
     return Album.objects.all() 


#=============DETAILS VIEW=============== details about one object 
class DetailView(generic.DetailView): 
    #what model are we looking at /for 
    model=Album 
    template_name='music/detail.html' 

#===============For the add album form 
class AlbumCreate(CreateView): 
    model=Album 
    fields=['artist','album_title','genre','album_logo'] 
    template_name='music/album_form.html' 

urls.pyのC ODE:

from django.contrib import admin 
from django.urls import include, path 

from . import views #the dot means look at the current directory - look for a module called views 

app_name='music' 

urlpatterns = [ 
    #this is matching /music/ 
    path('', views.IndexView.as_view(), name='index'), 
    #when you use a detail view it expects a primary key 
    path("<pk>/", views.DetailView.as_view(), name="detail"), 
    #/music/album/add - dont need to specify pk 
    path('album/add/', views.AlbumCreate.as_view(), name="album-add"), 
] 

誰もが問題を解決するために、エラーを見つけることができますか? album_form.htmlページに移動するには、「アルバムを追加」リンクが必要です。 music/templates/music/album_form.html(フォームテンプレートを含むalbum_formを含む)

+0

あなたの 'include'は音楽URLをどのように見えますか? –

答えて

1

「詳細」URLパターンはあまり一般的ではなく、「album-add」という文字列を含むすべてをキャプチャしています。 "<int:pk>/"で整数に制約し、アルバム追加パターンの後に移動させてください。

+0

ありがとう! pkに関連する別の質問に苦労していると私は思います。 – MissComputing

+0

https://stackoverflow.com/q/48007916/5074035 – MissComputing

関連する問題