1、不使用Form表單
獲取前端數據方式
name = request.POST['name']
name = request.POST.get['name']
2、使用Form表單
def comment_post(request): try: comment_form = if comment_form.is_valid(): comment = Comment.objects.create(username=comment_form.cleaned_data["author"], email=comment_form.cleaned_data["email"], url=comment_form.cleaned_data["url"], content=comment_form.cleaned_data["comment"], article_id=comment_form.cleaned_data["article"], user=request.user if request.user.is_authenticated() else None) comment.save() else: return render(request, 'failure.html', {'reason': comment_form.errors}) except Exception as e: logger.error(e) return redirect(request.META['HTTP_REFERER'])
3、Form表單驗證
方式1、表單字段驗證器
方式二:clean_filedname驗證字段,針對某個字段進行驗證
方式三:表單clean方法,可針對整個表單進行驗證。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。