简体   繁体   中英

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

This is my 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:

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.

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

The problem is, in your urlpatterns you put the same url two times:

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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