kurtgn
kurtgn

Reputation: 8732

django redirect to a view outside the app

I have a django project with two apps, users for login/logout, and timeline for main functionality. I need to redirect users that log in from users.log_in to timeline.dashboard view. But it doesn't work, all I see is 404.

as I see in the browser, Django wants to open http://localhost:8000/users/login/timeline.dashboard instead of http://localhost:8000/timeline/dashboard/

so what should I do?

In case it's needed - this is the code that I have:

project's urls.py

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^timeline/', include('timeline.urls')),
    url(r'^users/', include('users.urls')),
)

timeline/urls.py

urlpatterns = patterns(
    'timeline.views',
    url(r'^$', views.index, name='index'),
    url(r'^dashboard/$', views.dashboard, name='dashboard'),
)

users/views.py:

def log_in(request):
    if request.method=='GET':
        return render(request,
            'login.html')
    if request.method=='POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request,user)
            return redirect('timeline.dashboard')
        else:
            return redirect('log_in')

the app_name.view_name syntax the redirect args is invented by myself and obviously wrong, but I can't see anything helpful in the official docs.

Thanks!

Upvotes: 0

Views: 3289

Answers (2)

https://docs.djangoproject.com/en/1.7/topics/http/shortcuts/#redirect

Here are the 3 things attempted.

  1. A model: the model’s get_absolute_url() function will be called.
  2. A view name, possibly with arguments: urlresolvers.reverse will be used to reverse-resolve the name.
  3. An absolute or relative URL, which will be used as-is for the redirect location.

Attempt number 1 and 2 didn't return a URL, so redirect used its third method of interpreting your input: as is, as a relative URL.

You likely intended to match the 2. A view name, which in this case is simply dashboard.

The confusion comes from the fact this function attempts multiple URL lookup methods and hides errors (and obfuscates the docs to some extent).

For direct documentation on #2, look at the documentation for reverse.

https://docs.djangoproject.com/en/1.7/ref/urlresolvers/#django.core.urlresolvers.reverse

Upvotes: 3

Cui Heng
Cui Heng

Reputation: 1285

Are you running django in the standalone mode? The best practice is using WSGI to deploy django into apache, and setup WSGI in the httpd.conf to route request to the concrete apps for cross application works.

Upvotes: -1

Related Questions