NoReverseMatch : Reverse for 'download' not found. 'download' is not a valid view function or pattern name.

2021. 4. 10. 21:00파이썬/Django

/mp3/views.py

def index(request) :
    template = 'index.html'
    return render(request, template, None)

mp3/templates/index.html

<input type="text" size="50" value="">
<input type="button" value="다운로드" onclick="location.href='{% url 'mp3:download' %}'">

mp3/urls.py

app_name = 'mp3'
urlpatterns = [
    path('', views.index, name='index'),
]

index.html에서 찾는 download가 없기 대문에 에러 발생

아래와 같이 추가해줘야 함

views.py

def index(request) :
    template = 'index.html'
    return render(request, template, None)

def download(request) :
    return HttpResponseRedirect(reverse('mp3:index'))  

urls.py

app_name = 'mp3'
urlpatterns = [
    path('', views.index, name='index'),
    path('download/', views.download, name='download'),
]