【问题标题】:Django 2 - URL routing issueDjango 2 - URL 路由问题
【发布时间】: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 和迁移,并且该站点可以正常加载发布页面,并且数据和图像都可以正常加载。只是无法让索引页面正常工作,而且以​​前从未工作过。

如果有任何其他文件,请随时联系我,但我认为它在这些文件中的某个位置。我查看了文件顶部的文档以及官方文档,但也无法从那里得到任何工作。

【问题讨论】:

    标签: python django


    【解决方案1】:

    您注册了music/,但没有注册music/index/name= 引用仅在您需要通过 {% url 'music:index' %} 之类的方式调用模板中的软编码 URL 时使用。

    仅供参考,127.0.0.1:8000/music127.0.0.1:8000/music/ 是不同的路线。

    编辑

    我知道这不是你问题的一部分,而是这部分

    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"),
    

    可能容易出现错误。如果你有一个艺术家和一首同名的歌曲怎么办?比 Django 会有冲突的 URL,而且通常处理起来很烦人。只需在主键之前(分别)附加song/detail/ 之类的前缀。

    【讨论】:

    • 感谢您的回复,基本上当我转到:127.0.0.1:8000 我希望它加载我的 base.html 并嵌入我的 index.html 两者都在我的 music/templates/base.html 中music/templates/index.html 我知道 base.html 正在加载 reeases 页面,并且 index.html 那里有 html 内容,但路由器似乎无法找到它。试图做你在主 urls.py 的第一行中提到的,但我迷路了。 path('', include('music.urls')), path('music/index', include('music.urls')), path('music/index/', include('music.urls') ),
    • 我不是那个意思。你应该先写path: ('', include('music.urls')),然后写path('index/', views.IndexView.as_view()。第一个是全局的,第二个是应用中的 URL。
    • 感谢您的帮助。问题是我需要将视图更改为: class IndexView(generic.TemplateView): 谢谢额外的注释,我会看看我是否也能弄清楚。
    猜你喜欢
    • 2017-08-24
    • 2020-10-09
    • 2021-07-24
    • 1970-01-01
    • 2013-09-11
    • 2017-03-26
    • 2016-04-15
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多