我最近完成了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
时,网址标记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'),
)
答案 0 :(得分:1)
您似乎以错误的方式定义了网址。
应该是这样的:
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)),
)
然后在app的urls.py
文件中,网址应定义为:
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
)