从 Django 应用程序重定向 url
Redirect url from django application
我有一个用 Django 构建的基本应用程序,它只有在我输入以下内容后才能运行:
http://xx.xx.xxx.xx/polls
。我如何在我的 urls.py 文件中重写它,以便 http://xx.xx.xxx.xx/polls
将我重定向到 http://xx.xx.xxx.xx/
?
我的 urls.py 主项目文件:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
我的 urls.py 来自应用程序的文件:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
]
我的项目结构:
├── blog
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── db.sqlite3
├── manage.py
├── polls
│ ├── admin.py
│ ├── admin.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── templates
│ │ └── polls
│ │ ├── detail.html
│ │ ├── index.html
│ │ ├── results.html
│ │ └── static
│ │ └── polls
│ │ ├── css
│ │ │ ├── semantic.css
│ │ │ ├── semantic.min.css
│ │ │ ├── sidebar.css
│ │ │ ├── sidebar.min.css
│ │ │ ├── sidebar.min.js
│ │ │ └── style.css
│ │ ├── images
│ │ └── js
│ │ ├── jquery-1.11.2.min.js
│ │ ├── semantic.js
│ │ ├── semantic.min.js
│ │ ├── sidebar.js
│ │ └── sidebar.min.js
│ ├── tests.py
│ ├── tests.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── readme.txt
├── requirements.txt
├── templates
│ └── admin
│ └── base_site.html
据我了解,您不想从 /polls/
重定向到 /
,而是想在主页上显示投票索引。如果是这样,那么只需将 index
url 从 polls/urls.py
移动到主 urls.py
:
from polls.views import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view(), name='index'),
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
UPDATE:在 django templates/code 中使用硬编码 url 是一种不好的做法。您应该始终使用 {% url %}
template tag and the reverse()
函数。这将允许您根据需要更改 urls 而不会破坏代码。
所以link到索引页应该是这样的:
<a href="{% url 'index' %}">Home page</a>
并且,例如,link 投票详情:
<a href="{% url 'polls:detail' poll.pk %}">Poll #{{ poll.pk }}</a>
在模特的get_absolute_url()
method use the reverse()
.
您还可以通过以下操作将整个投票应用包含在“/”中:
url(r'^', include('polls.urls', namespace="polls")),
此外,请参阅此处了解有关 urls.py 中重定向的更多信息:
Redirect to named url pattern directly from urls.py in django?
在你的情况下它会是这样的:
from django.views.generic import RedirectView
url(r'^polls/', RedirectView.as_view(pattern_name='polls:index')
我有一个用 Django 构建的基本应用程序,它只有在我输入以下内容后才能运行:
http://xx.xx.xxx.xx/polls
。我如何在我的 urls.py 文件中重写它,以便 http://xx.xx.xxx.xx/polls
将我重定向到 http://xx.xx.xxx.xx/
?
我的 urls.py 主项目文件:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
我的 urls.py 来自应用程序的文件:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
]
我的项目结构:
├── blog
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── db.sqlite3
├── manage.py
├── polls
│ ├── admin.py
│ ├── admin.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── templates
│ │ └── polls
│ │ ├── detail.html
│ │ ├── index.html
│ │ ├── results.html
│ │ └── static
│ │ └── polls
│ │ ├── css
│ │ │ ├── semantic.css
│ │ │ ├── semantic.min.css
│ │ │ ├── sidebar.css
│ │ │ ├── sidebar.min.css
│ │ │ ├── sidebar.min.js
│ │ │ └── style.css
│ │ ├── images
│ │ └── js
│ │ ├── jquery-1.11.2.min.js
│ │ ├── semantic.js
│ │ ├── semantic.min.js
│ │ ├── sidebar.js
│ │ └── sidebar.min.js
│ ├── tests.py
│ ├── tests.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── readme.txt
├── requirements.txt
├── templates
│ └── admin
│ └── base_site.html
据我了解,您不想从 /polls/
重定向到 /
,而是想在主页上显示投票索引。如果是这样,那么只需将 index
url 从 polls/urls.py
移动到主 urls.py
:
from polls.views import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view(), name='index'),
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
UPDATE:在 django templates/code 中使用硬编码 url 是一种不好的做法。您应该始终使用 {% url %}
template tag and the reverse()
函数。这将允许您根据需要更改 urls 而不会破坏代码。
所以link到索引页应该是这样的:
<a href="{% url 'index' %}">Home page</a>
并且,例如,link 投票详情:
<a href="{% url 'polls:detail' poll.pk %}">Poll #{{ poll.pk }}</a>
在模特的get_absolute_url()
method use the reverse()
.
您还可以通过以下操作将整个投票应用包含在“/”中:
url(r'^', include('polls.urls', namespace="polls")),
此外,请参阅此处了解有关 urls.py 中重定向的更多信息: Redirect to named url pattern directly from urls.py in django?
在你的情况下它会是这样的:
from django.views.generic import RedirectView
url(r'^polls/', RedirectView.as_view(pattern_name='polls:index')