Reputation: 413
I'm building a social network and I want to show special content when a user is logged in and he accesses to his public profile url (so i'll show customization tools). I've written code to return the user name and match it with the regex, but I don't know how to only have the pattern if the user is logged in.
from django.conf.urls import patterns, include, url
import re
from auth import engine
profile_name = engine.get_profile_name()
urlpatterns = patterns('',
...
url(r'^'+re.escape(profile_name)+r'/?', 'myprofile.views.show_profile') # authentication required
)
The engine will return None
if the user is not logged in. But this may cause an error in url().
So how can I achieve it?
Upvotes: 0
Views: 90
Reputation: 47172
You have to decorate either your view function or view class with login_required
.
There is no regex way to find out if the user is logged in or not since it's handled by the sessions and requests and not your url.
You can read up on it here otherwise here's an example functional view
@login_required(login_url="/login/") #will redirect to login if not logged in.
def show_profile(request, profile_name):
return render_to_response(...)
Or this is another approach if you want to omit the decorator
def show_profile(request, profile_name):
if request.user.is_authenticated():
return render_something_cool
else:
return render_something_else
Upvotes: 3