【问题标题】:Redirect url from django application从 django 应用程序重定向 url
【发布时间】:2015-06-19 15:40:02
【问题描述】:

我有一个用 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  

【问题讨论】:

    标签: django url-rewriting django-urls


    【解决方案1】:

    据我所知,您不会从/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)),
    ] 
    

    更新:在 django 模板/代码中使用硬编码的 url 是一种不好的做法。您应该始终使用{% url %} 模板标签和reverse() 函数。这将允许您在不破坏代码的情况下根据需要更改 url。

    所以到索引页面的链接应该是这样的:

    <a href="{% url 'index' %}">Home page</a>
    

    例如,链接到投票详情:

    <a href="{% url 'polls:detail' poll.pk %}">Poll #{{ poll.pk }}</a>
    

    在模型的get_absolute_url() 方法中使用reverse()

    【讨论】:

    • 好的,它可以工作@catavaran,但问题是其他链接不起作用(他们会将我重定向到错误页面)。这就是我想要重定向的原因。
    • 我应该如何修改其他链接,以便您的方法适用于其他页面。您的解决方案适用于第一页,但如果我尝试单击该页面中的链接,则会出现其他页面的错误
    • 您没有要重定向的页面。 / url 没有任何关联的视图。这就是为什么我建议将此网址映射到您的IndexView。至于断开的链接..请参阅我的答案中的更新。始终为链接使用 django 的 url 解析器。
    【解决方案2】:

    您还可以通过以下方式将整个投票应用程序包含在“/”中:

        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')
    

    【讨论】:

    • url(r'^', include('polls.urls', namespace="polls")), 这就是我要找的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2013-03-15
    • 2021-10-08
    • 1970-01-01
    • 2017-08-02
    相关资源
    最近更新 更多