我有博客系统,每个用户都有自己的主页;
127.0.0.1/用户名
每个用户的登录屏幕位于:127.0.0.1/username/panel
这里又是他们的管理面板:127.0.0.1/username/admin
在登录页面中;当我提交时;它必须将用户重定向到管理面板。
但我需要访问目前在 url 中的用户名。我想我应该使用正则表达式。但如何?我对正则表达式不好。
这是我的 urls.py:
urlpatterns = patterns('blog.views',
url(r'^blog/(?P<username>[-\w]+)/$',view='index', name='index'),
url(r'^blog/(?P<username>[-\w]+)/post/(?P<postslug>[-\w]+)',view='singlePost', name='view_blog_post'),
url(r'^blog/(?P<username>[-\w]+)/panel/$', view='loguserin'),
url(r'^blog/(?P<username>[-\w]+)/admin/$', view='adminView', name='admin'),
url(r'^blog/(?P<username>[-\w]+)/admin/loggedout/$', view='logout', name='logout'),
url(r'^blog/(?P<username>[-\w]+)/admin/addpost/$', view='addpost',name='addpost'),
url(r'^blog/(?P<username>[-\w]+)/admin/editpost/(?P<post_id>\d+)', view='editPost', name='editpost'),
url(r'^blog/(?P<username>[-\w]+)/admin/delete/(?P<post_id>\d+)', view='delete_post', name='deletePost'),
)
这是我的views.py [登录部分]:
def loguserin(request,username):
if request.method=='POST':
uname = request.POST['username']
password = request.POST['password']
user = authenticate(username=uname, password=password)
form = LoginForm(request.POST)
if form.is_valid():
if user is not None:
request.session['loggedin']="djangoo"
login(request, user)
return HttpResponseRedirect('/admin/') #--> ????
else:
form = LoginForm()
return render_to_response('login.html',{'form':form,},context_instance=RequestContext(request))