2017-12-28 6 views
0

Django 프로젝트의 base.html에 지정된 "Add Album"에 대한 링크가 있습니다. 은 "앨범 추가"링크 그러나 클릭하면 코드는Django Project : 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

음악 URL을 '포함'하는 방법은 무엇입니까? –

답변

1

"세부 사항"URL 패턴이 너무 일반적이어서 'album-add'문자열을 포함하여 모든 것을 캡처합니다. "<int:pk>/"으로 정수로 제한하거나 앨범 추가 패턴 다음으로 이동하십시오.

+0

고마워요! pk와 관련된 다른 질문에 어려움을 겪고 있다고 생각합니다. – MissComputing

+0

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

관련 문제