首页 > 解决方案 > Django 在错误的地址使用格式错误的 urls.py 提供错误的模板(编辑器应用程序)

问题描述

标签: pythonhtmldjango

解决方案


Few things to notice is the urlpatterns :

path('', include('posts.urls')),

Because this URL is on top Django will first look here.

In the post app you also have an urls.py that looks like the following

path('', views.posts, name='posts'),

This matches you 127.0.0.1:8000/ so this will be your homepage.

In the landings template you post to :

form action="{% url 'home' %}" method="get">

The home which is a different view in a different app.

If you want to first serve the Home landing page you need to do the following

First Change the order in your urls.py like the following:

urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include('redactors.urls')), 1
   path('', include('posts.urls')), 2
   path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Second

In your redactors app go to your urls.py and change ’home’ to ’’</p>

This will make sure that you always start with the home page.

The last problem you are facing is that you are missing data when you post/get to the home view.

This is because you do not give back :

context = {'posts':posts}

in the context to the render function in your home view. So the template cannot render posts that are not available.


推荐阅读