How to redirect to named url pattern directly from urls py in django

0 votes
n Django, how can I do a simple redirect directly from urls.py?  I would like to get the target based on it's named url pattern, rather than hard coding the url.
Aug 12, 2020 in Python by kartik
• 37,520 points
621 views

1 answer to this question.

0 votes

Hello @kartik,

You can try this:

from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^some-page/$', RedirectView.as_view(url='/')),
    ...

In above example '/' means it will redirect to index page, where you can add any url patterns also.

Hope it helps!!
Thank you!

answered Aug 12, 2020 by Niroj
• 82,760 points

Related Questions In Python

0 votes
1 answer

How to clear a database from the CLI with manage.py in Django?

Hello @kartik, To drop the database and run syncdb again. ...READ MORE

answered Aug 7, 2020 in Python by Niroj
• 82,760 points
642 views
0 votes
1 answer

How to generate urls in django?

If you need to use something similar ...READ MORE

answered Apr 12, 2019 in Python by SDeb
• 13,290 points
390 views
0 votes
1 answer

How to output text from database with line breaks in a django template?

Hello @kartik, Use linebreaks or linebreaksbr filter: {{ text|linebreaks }} Or surround the text ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,760 points
3,149 views
0 votes
1 answer

How to get value from form field in django framework?

Hello @kartik, Using a form in a view can ...READ MORE

answered Jun 30, 2020 in Python by Niroj
• 82,760 points
3,086 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,760 points
836 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,760 points
349 views
0 votes
1 answer

How to order_by a JSON from serializers.py file in django rest framework?

Hello @kartik, There's an easy way, just override ...READ MORE

answered Jul 1, 2020 in Python by Niroj
• 82,760 points
1,001 views
0 votes
1 answer