我想在我的网站上运行我的album.object.all()编码。我已经包含了数据,但是将其放入urls.py文件后,它没有找到响应的路径。请帮助我。
这是django 1.11.2
我的urls.py
文件
from django.conf.urls import url
from django.contrib import admin
from myapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
url(r'^album/?$', views.music, name='music'),
url(r'^album/(?P<album_id>\d+)/$', views.detail, name='detail'),
url(r'^Database/?$', views.Database, name='Database'),
]
我的views.py
文件
from django.shortcuts import render
from django.http import HttpResponse
from myapp.models import Album
# Create your views here.
def home(request):
return render(request, 'index.html')
def music(request):
all_albums = Album.objects.all()
context ={'all_albums': all_albums,}
return render(request, 'album.html', context)
def detail(request, album_id):
return HttpResponse("<h2>Details for album id: " + str(album_id) + "</h2>")
def Database(request):
return render(request, 'data.html')
我的model.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)
#any songs of the related to the deleted album will be deleted
file_type = models.CharField(max_length=10)
song_title = models.CharField(max_length=200)
要使用它,请通过<pk>=album_id