Reputation: 8732
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
Reputation: 118518
https://docs.djangoproject.com/en/1.7/topics/http/shortcuts/#redirect
Here are the 3 things attempted.
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
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