0

I have a basic application built in Django, which only works if I enter:

http://xx.xx.xxx.xx/polls. How can I rewrite this in my urls.py file so that http://xx.xx.xxx.xx/polls will redirect me to http://xx.xx.xxx.xx/ ?

My urls.py file for the main project:

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)),
] 

My urls.py file from the application:

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

My project structure:

├── 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  

2 Answers 2

3

As far as I undesrstand you don't ant to redirect from /polls/ to / but want to show the polls index at the home page. If so then just move the index url from the polls/urls.py into the main 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: It is a bad practice to use hard-coded urls in the django templates/code. You should always use the {% url %} template tag and the reverse() function. This will allow you to change urls as you want without breaking the code.

So link to the index page should be like this:

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

And, for example, link to the poll details:

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

In the model's get_absolute_url() method use the reverse().

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, it works @catavaran but the problem is that the other links won't work (they will redirect me to an error page) . That's why I wanted a redirect.
how should I modify the other links so that your method will work for other pages. Your solution is ok for the first page, but if I try to click a link from that page, it will give me error for the other pages
You have no page to redirect. The / url doesn't have any associated view. This is why I suggest to map this url to your IndexView. As for broken links.. see the update in my answer. Always use django's url resolver for the links.
1

You could also include your whole polls app on '/' by doing:

    url(r'^', include('polls.urls', namespace="polls")),

Also, see here for more information on redirecting in urls.py: Redirect to named url pattern directly from urls.py in django?

In your case it would be something like:

    from django.views.generic import RedirectView

    url(r'^polls/', RedirectView.as_view(pattern_name='polls:index')

1 Comment

url(r'^', include('polls.urls', namespace="polls")), this was what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.