gpt4 book ai didi

python - 如何在 Django 中正确设置 URLConf(正则表达式?)

转载 作者:太空宇宙 更新时间:2023-11-04 05:46:54 25 4
gpt4 key购买 nike

我最近完成了 Django Tutorial现在正在开发我自己的网络应用程序。我遇到的问题是为此应用程序设置 URLConf。我仍然不完全理解 RegEx 匹配以便将不同的页面相互链接(或者我只是忘记了一些简单的事情??)

我尝试以某种方式设置 URLConf,以便当我单击每个页面上的按钮时,它会转到下一页(总共 5 个)。

它应该是这样的

第 0 页( http://127.0.0.1:8000/ )

第 1 页( http://127.0.0.1:8000/page1/ )

并继续这种模式(http://127.0.0.1:8000/page2/http://127.0.0.1:8000/page3/http://127.0.0.1:8000/page4/)

当我在第 0 页上单击 Next Page 时,它将转到第 1 页。当我在第 1 页上单击 Submit 时,URL slug page1 更改为 page2 但仍显示相同的 html 页面。

这是我的两个 urls.py 文件:

"""qfdjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

# builds URLS for all across the site
# decoupled from mainsite.urls
urlpatterns = patterns('',
url(r'^', include('mainsite.urls')),
url(r'^someOtherPage/', include('mainsite.urls')),
url(r'^anotherPage/', include('mainsite.urls')),
url(r'^page1/', include('mainsite.urls')),
url(r'^page2/', include('mainsite.urls')),
url(r'^page3/', include('mainsite.urls')),
url(r'^page4/', include('mainsite.urls')),
url(r'^admin/', include(admin.site.urls)),
)

"""qfdjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import patterns, include, url

# these patterns are for specific sections of the site
# the 'primary' URLs are located in qfdjango.urls
# URL design of an app is specific to the app,
# not the whole Django project
urlpatterns = patterns('mainsite.views',
url(r'^$', 'index'),
url(r'$', 'page_1'),
url(r'$', 'page_2'),
url(r'$', 'page_3'),
url(r'$', 'page_4'),
)

最佳答案

您似乎以错误的方式定义了 url。

应该是这样的:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
url(r'^', include('mainsite.urls')), # include your app urls once
url(r'^admin/', include(admin.site.urls)),
)

然后在应用程序的 urls.py 文件中,url 应该这样定义:

from django.conf.urls import patterns, include, url

urlpatterns = patterns('mainsite.views',
url(r'^$', 'index'),
url(r'^someOtherPage/$', 'someOtherPage'),
url(r'^anotherPage/$', 'anotherPage'),
url(r'page1/$', 'page_1'), # page 1 url
url(r'page2/$', 'page_2'), # page 2 url
url(r'page3/$', 'page_3'), # page 3 url
url(r'page4/$', 'page_4'), # page 4 url
)

关于python - 如何在 Django 中正确设置 URLConf(正则表达式?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com