简体   繁体   English

Django 不同 url 去同一个页面

[英]Django different url going to the same page

I am trying to create 2 extra pages on my Django site, I created the first one with no problem (calendar.html) but when I try to create the second one (actionplan.html) it gives me no error, but when I access xxx/actionplan.html, it shows the calendar.html page... I cannot access xxx/actionplan.html我正在尝试在我的 Django 站点上创建 2 个额外页面,我创建了第一个页面没有问题(calendar.html)但是当我尝试创建第二个页面(actionplan.html)时它没有给我任何错误,但是当我访问xxx/actionplan.html,它显示日历。html页面...我无法访问xxx/actionplan.html

This is my urls.py:这是我的 urls.py:

from django.contrib import admin
from django.urls import path, include

from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static


from django.views.generic import TemplateView
from django.views.generic.detail import DetailView

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('blog.urls')),
        url(r'xxx', TemplateView.as_view(template_name="calendar.html")),
        url(r'^xxx/$', DetailView.as_view(template_name="actionplan.html")),
        url(r'^admin/', admin.site.urls),
        url(r'^', include('blog.urls'), name="Blog"),
        ] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

This is my views.py:这是我的views.py:

from django.views import generic
from .models import Post

class PostList(generic.ListView):
    queryset = Post.objects.filter(status=1).order_by('-created_on')
    template_name = 'index.html'

class PostDetail(generic.DetailView):
    model = Post
    template_name = 'post_detail.html'

class Calendar(generic.DetailView):
   model = Post
   template_name = 'calendar.html'

class Planoacao(generic.DetailView):
   model = Post
   template_name = 'actionplan.html'

I have tried:我努力了:

 url(r'^xxx/$', DetailView.as_view(template_name="actionplan.html")),
    url(r'^xxx', DetailView.as_view(template_name="actionplan.html")),
    url(r'^xxx$', DetailView.as_view(template_name="actionplan.html")),
    url(r'xxx', DetailView.as_view(template_name="actionplan.html")),

I am officially out of ideas now... can anyone spot a problem?我现在正式没有想法......有人能发现问题吗?

You've given them the same url essentially, yoursite.com/xxx , you could reorder them and put the one with the slash first and that might work but then that would make it a nightmare if you use django's APPEND_SLASH setting.你已经给了他们相同的 url 本质上, yoursite.com/xxx ,你可以重新排序它们并将斜线放在第一位,这可能会起作用,但是如果你使用 django 的APPEND_SLASH设置,那将成为一场噩梦。

To fix, make your urls unique要解决此问题,请让您的网址独一无二

The actionplan.html has nothing to do with your url, its "working" only because the regex for the calendar is just looking for xxx in the given url actionplan.html与您的 url 无关,它的“工作”只是因为日历的正则表达式只是在给定的 Z572D4E421E5E6B9BC11D815E8A027 中寻找xxx

The problem is, in your urlpatterns you put the same url two times:问题是,在您的urlpatterns中,您将相同的 url 放了两次:

url(r'xxx', TemplateView.as_view(template_name="calendar.html")),
url(r'^xxx/$', DetailView.as_view(template_name="actionplan.html")),

You should add a different url for the two views,您应该为这两个视图添加不同的 url,

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
    url(r'^xxx/plan$', DetailView.as_view(template_name="actionplan.html")),
    url(r'xxx', TemplateView.as_view(template_name="calendar.html")),
    url(r'^admin/', admin.site.urls),
    url(r'^', include('blog.urls'), name="Blog"),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

and you should use path no url and not both你应该使用path没有 url 而不是两者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM