【发布时间】:2020-07-25 05:55:51
【问题描述】:
我试图让我的索引页面在我的 Django 2 项目上打开,但我似乎无法从我的音乐应用程序 (index.html) 中的模板中打开一个简单的页面。目前使用基于类的视图。
我的发布 URL 有效,但使用的是他的 re_path。我似乎根本无法正常工作。
我已经有一年没看这个项目了,也没有正常使用 Django,但想重新开始。
urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# include urls from the music app
path('music/', include('music.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
音乐/urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from . import views
# defined the app name in case the same fields are used in other apps
app_name = 'music'
urlpatterns = [
# no info past music return index EG /music/
path('', views.IndexView.as_view(), name='index'),
re_path(r'^release/$', views.ReleaseView.as_view(), name='release'),
re_path(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name="detail"),
re_path(r'^(?P<pk>[0-9]+)/$', views.ArtistView.as_view(), name="artist"),
re_path(r'^register/$', views.UserFormView.as_view(), name="register"),
]
views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.views import generic
from django.views.generic import ListView, View
from .models import Artist, Track, Release
from .forms import UserForm
from urllib import request
class IndexView(View):
template_name = 'music/index.html'
# def get(self, request):
# return render(request, "music/index.html")
class ReleaseView(generic.ListView):
template_name = 'music/releaselist.html'
context_object_name = 'all_releases'
def get_queryset(self):
return Release.objects.all()
class ArtistView(generic.ListView):
model = Artist
template_name = 'music/base.html'
context_object_name = 'all_artists'
def get_queryset(self):
artist_name = Artist.objects.all()
return render(request, "music/base.html", {'artist_name': artist_name})
# return render(request, "music/base.html", {'artists': artists})
class DetailView(generic.DetailView):
model = Release
template_name = 'music/detail.html'
class UserFormView(View):
form_class = UserForm
template_name = 'music/registration_form.html'
# blank form
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
# cleaned normalised data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
# returns user objects if credential are valid
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('music:index')
return render(request, self.template_name, {'form': form})
我的 base.html 和 index.html 位于 recordlabel/music/templates
如果有人能发现任何对我有很大帮助的东西,因为我知道这是我没有做的愚蠢的事情。我已经尝试过 makemigrations 和迁移,并且该站点可以正常加载发布页面,并且数据和图像都可以正常加载。只是无法让索引页面正常工作,而且以前从未工作过。
如果有任何其他文件,请随时联系我,但我认为它在这些文件中的某个位置。我查看了文件顶部的文档以及官方文档,但也无法从那里得到任何工作。
【问题讨论】: