Ablivion
Ablivion

Reputation: 121

How to redirect in django while using django-hosts?

I'm using django-hosts package to manage content that's supposed to be set in various different subdomains. I started by making a view in which a form is used to change something. Once the form is validated and processed, user is supposed to be redirected to some other page. Looking through documentation there's an easy way to render():

settings_url = reverse('settings', host='dashboard')
return render(request, 'dashboard/settings.html', {'settings_url': settings_url})

However, there's no mention of redirect(). So how would I go about redirecting to somewhere else, instead of the usual return redirect("dashboard:settings")

myapp/hosts.py :

host_patterns = patterns('',
    host(r'www', settings.ROOT_URLCONF, name='www'),
    host(r'dashboard\.myapp\.com', 'dashboard.urls', name='dashboard'),
)

dashboard/urls.py

from .views import home, websitesettings

urlpatterns = [
    url(r'^$', home, name='home'),
    url(r'^settings/$', websitesettings, name='settings'),
]

Basically all I want is to redirect back to the same page after form is submitted (in the render example, I'm submitting a form to change certain website settings, which is a model on it's own, and then I want to redirect back to the same page after that).

Upvotes: 2

Views: 2141

Answers (1)

Ablivion
Ablivion

Reputation: 121

Well took couple of different and convoluted tries, only to take a night off and come up with a solution that's ironically easy. Use of dajngos native redirect with django-hosts reverse function and it works like a charm:

from django_hosts.resolvers import reverse

return redirect(reverse('home', host='dashboard'))

Upvotes: 3

Related Questions