我做了一個(gè)小項(xiàng)目。用戶首先登錄,然后他被重定向到主頁。但是,如果用戶尚未登錄,那么如果我們粘貼該主頁的 url,該主頁也會(huì)打開。我已經(jīng)嘗試了網(wǎng)上給出的很多東西,但它不起作用。這是我的 urls.pyfrom django.conf.urls import urlfrom django.contrib import adminfrom blog import viewsurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', views.login, name='login'), url(r'^register/$', views.register, name='register'), url(r'^home/$', views.home, name='home'), url(r'^javaq/$', views.javaq, name='javaq'), url(r'^javar/$', views.javar, name='javar'), url(r'^csq/$', views.csq, name='csq'), url(r'^csr/$', views.csr, name='csr'), url(r'^cq/$', views.cq, name='cq'), url(r'^cr/$', views.cr, name='cr'), url(r'^cppq/$', views.cppq, name='cppq'), url(r'^cppr/$', views.cppr, name='cppr'), url(r'^pythonq/$', views.pythonq, name='pythonq'), url(r'^pythonr/$', views.pythonr, name='pythonr'), url(r'^mini/$', views.mini, name='mini'),]這是我的view.pyfrom django.shortcuts import renderfrom .models import logininfo,CSTEST,CTEST,CPPTEST,JAVATEST,PYTHONTEST,resultfrom django.http import HttpResponsefrom django.shortcuts import renderfrom django.contrib.auth.decorators import login_requiredfrom django.views.decorators.cache import cache_controlimport datetimedef login(request): if request.method=="POST": user_name=request.POST.get("username") sid=request.POST.get("sid") password=request.POST.get("password") article = logininfo() article.user_name = user_name article.sid = sid article.password = password article.save() return render(request,'login.html')def home(request): if request.method=="POST": sid=request.POST.get("sid") password=request.POST.get("password") request.session['sd'] = sid user_obj=logininfo.objects.filter(sid=sid,password=password) if user_obj.count()==0: return HttpResponse("Sorry wrong password try again") return render(request,'home.html') def register(request): return render(request,'register.html')
查看完整描述