登陆 全站 user
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'mysite.VAR.LANG', ], }, }, ]
def LANG(request): try: username=request.session['user_name'] userid=request.session['user_id'] is_login=request.session['is_login'] except Exception as e: username=None userid=None is_login=False lang={ 'SITE':'笑话大全', 'username':username, 'userid':userid, 'is_login':is_login, } return lang
class LoginView(View): def get(self,request): return render(request,'login.html') def post(self,request): # if request.session.get('is_login', None): # return redirect("") username=request.POST.get('username',None) password=request.POST.get('password',None) user=authenticate(request,username=username,password=password) if user is not None: login(request,user) request.session['is_login'] = True request.session['user_id'] = str(user.id) request.session['user_name'] = str(user) else: return HttpResponse(u'密码不对或者不存在') return render(request,'login.html')
class IndexView(View): @method_decorator(login_required(login_url='login/')) def get(self,request): types=TypeSheet.objects.all() tags=TagSheet.objects.all() articles=ArticleSheet.objects.all()[:20] context={ 'types':types, 'tags':tags, 'articles':articles, } return render(request, 'index.html', context)
{% load static %} <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/html"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>{{ title }} - {{ SITE }}</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}" /> </head> <body> <div class="main-container"> 欢迎 name:{{ username }},id:{{ userid }},login:{{ is_login }},