def posting(request): if request.user.is_authenticated(): username = request.user.username useremail = request.user.email messages.get_messages(request) if request.method == 'POST': user = User.objects.get(username=username) diary = models.Diary(user=user) post_form = forms.DiaryForm(request.POST, instance=diary) if post_form.is_valid(): messages.add_message(request, messages.INFO, 'message was stored. ') post_form.save() return HttpResponseRedirect('/') else: messages.add_message( request, messages.INFO, 'if you want to post a message, you should fill all the blanks. ' ) else: post_form = forms.DiaryForm() messages.add_message( request, messages.INFO, 'if you want to post a message, you should fill all the blanks. ') template = get_template('posting.html') html = template.render(context=locals(), request=request) return HttpResponse(html)
def posting(request): if request.user.is_authenticated(): username = request.user.username useremail = request.user.email messages.get_messages(request) if request.method == 'POST': user = User.objects.get(username=username) diary = models.Diary(user=user) post_form = forms.DiaryForm(request.POST, instance=diary) if post_form.is_valid(): messages.add_message(request, messages.INFO, "日記已儲存") post_form.save() return HttpResponseRedirect('/') else: messages.add_message(request, messages.INFO, '要張貼日記,每一個欄位都要填...') else: post_form = forms.DiaryForm() messages.add_message(request, messages.INFO, '要張貼日記,每一個欄位都要填...') template = get_template('posting.html') request_context = RequestContext(request) request_context.push(locals()) html = template.render(request_context) return HttpResponse(html)
def posting(request): #login? if request.user.is_authenticated(): #get curent login user info from auth.models User username = request.user.username useremail = request.user.email messages.get_messages(request) if request.method == 'POST': #WRITE #get login user info user = User.objects.get(username=username) #create instance Diary for specific user diary = models.Diary(user=user) #container [form DiaryForm] save [diary instance(user post)] post_form = forms.DiaryForm(request.POST, instance=diary) if post_form.is_valid(): messages.add_message(request, messages.INFO, "DIARY is stored ") post_form.save() return HttpResponseRedirect('/') else: messages.add_message(request, messages.INFO, "EVERY column need to fill !!!") else: #READ post_form = forms.DiaryForm() messages.add_message(request, messages.INFO, "EVERY column need to fill !!!") template = get_template('posting.html') html = template.render(locals(), request) return HttpResponse(html)
def posting(request): if request.user.is_authenticated: username = request.user.username useremail = request.user.email messages.get_messages(request) if request.method == 'POST': user = User.objects.get(username=username) diary = models.Diary(user=user) post_form = forms.DiaryForm(request.POST, instance=diary) if post_form.is_valid(): messages.add_message(request, messages.INFO, '日記已儲存') post_form.save() return HttpResponseRedirect('/') else: messages.add_message(request, messages.INFO, '要張貼日記,每一個欄位都要填寫呦') else: post_form = forms.DiaryForm() messages.add_message(request, messages.INFO, '要張貼日記,每一個欄位都要填寫呦') return render(request, 'posting.html', locals())
def posting(request): if request.user.is_authenticated: username = request.user.username useremail = request.user.email messages.get_messages(request) if request.method == 'POST': fuser = User.objects.get(username=username) form = forms.DiaryForm(request.POST) # diary = models.Diary(user=user) # post_form = forms.DiaryForm(request.POST, instance=diary) # if post_form.is_valid(): if form.is_valid(): try: fbudget = form.cleaned_data['budget'] fweight = form.cleaned_data['weight'] fnote = form.cleaned_data['note'] fdate = request.POST.get("DiaryDate", None) ds = fdate.split('/') fdate = ds[2] + '-' + ds[0] + '-' + ds[1] Diary = models.Diary.objects.create(user=fuser, budget=fbudget, weight=fweight, note=fnote, ddate=fdate) Diary.save() messages.add_message(request, messages.INFO, "日記已儲存") # post_form.save() return HttpResponseRedirect('/') # else: except Exception as e: print(e) messages.add_message(request, messages.WARNING, "Something worng!") # print(budget) else: post_form = forms.DiaryForm() messages.add_message(request, messages.INFO, "請寫下日記!") return HttpResponse(render(request, '../templates/posting.html', locals()))
def posting(request): if request.user.is_authenticated: username = request.user.username useremail = request.user.email messages.get_messages(request) if request.method == 'POST': user = auth.models.User.objects.get(username=username) diary = models.Diary(user=user) post_form = forms.DiaryForm(request.POST, instance=diary) if post_form.is_valid(): messages.add_message(request, messages.INFO, "日记已存储") post_form.save() return HttpResponseRedirect(reverse('index')) else: messages.add_message(request, messages.INFO, '如果要张贴信息,那么每一个字段都要填...') else: post_form = forms.DiaryForm() messages.add_message(request, messages.INFO, '如果要张贴日记,每一个字段都要填...') return render(request, 'posting.html', locals())
def diarypost(request): if request.user.is_authenticated: username = request.user.username useremail = request.user.email messages.get_messages(request) print(messages) if request.method == 'POST': user = User.objects.get(username=username) #先去auth那邊找出使用者資訊放到user裡面 diary = models.Diary(user=user) #再去建立一個該使用者的diary post_form = forms.DiaryForm(request.POST, instance=diary) #↑ 把表單裡面的DiaryForm帶入使用者與web傳回來的diary資訊 if post_form.is_valid(): #isvlid() 檢查表單有無資料與正確性 messages.add_message(request, messages.INFO, "日記已儲存") post_form.save() return HttpResponseRedirect('/') else: messages.add_message(request, messages.INFO, "每個欄位都要填寫") else: post_form = forms.DiaryForm() messages.add_message(request, messages.INFO, '每個欄位都要填寫喔') return render(request, 'diarypost.html', locals())