def add_news_category(request): name = request.POST.get('name') exists = NewsCategory.objects.filter(name=name).exists() if not exists: NewsCategory.objects.create(name=name) return restful_res.success() else: return restful_res.params_error(message='该分类已经存在')
def sms_captcha(request): telephone = request.GET.get('telephone') text = Captcha.gene_text() cache.set(telephone,text.lower(),5*60) print('短信验证码:',text) send_sms(telephone,text) return restful_res.success()
def register_view(request): form = RegisterForm(request.POST) if form.is_valid(): telephone = form.cleaned_data.get('telephone') username = form.cleaned_data.get('username') password = form.cleaned_data.get('password1') user = User.objects.create_user(telephone=telephone,username=username,password=password) login(request,user) return restful_res.success() else: return restful_res.params_error(message=form.get_errors())
def post(self, request): form = EditNewsForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') desc = form.cleaned_data.get('desc') content = form.cleaned_data.get('content') thumbnail = form.cleaned_data.get('thumbnail') pk = form.cleaned_data.get('pk') category_id = form.cleaned_data.get('category') category = NewsCategory.objects.get(pk=category_id) News.objects.filter(pk=pk).update(title=title, desc=desc, content=content, thumbnail=thumbnail, author=request.user, category=category) return restful_res.success() else: return restful_res.params_error(message=form.get_errors())
def post(self, request): print(0) form = WriteNewsForm(request.POST) print(1) if form.is_valid(): print(2) title = form.cleaned_data.get('title') desc = form.cleaned_data.get('desc') content = form.cleaned_data.get('content') thumbnail = form.cleaned_data.get('thumbnail') category_id = form.cleaned_data.get('category') category = NewsCategory.objects.get(pk=category_id) print(title, desc, content, thumbnail) News.objects.create(title=title, desc=desc, content=content, thumbnail=thumbnail, author=request.user, category=category) return restful_res.success() else: return restful_res.params_error(message=form.get_errors())
def login_view(request): form = LoginForm(request.POST) if form.is_valid(): telephone = form.cleaned_data.get('telephone') password = form.cleaned_data.get('password') remember = form.cleaned_data.get('remember') user = authenticate(request,username=telephone,password=password) if user: if user.is_active: login(request,user) if remember: request.session.set_expiry(None) #None默认过期时间2周 else: request.session.set_expiry(0) return restful_res.success() else: return restful_res.unauth(message="您的账户被冻结") else: return restful_res.params_error(message="用户名或者密码错误") else: errors = form.get_errors() return restful_res.params_error(message=errors)