def userinfo(request): if request.user.is_authenticated(): username = request.user.username user = User.objects.get(username=username) try: profile = models.Profile.objects.get(user=user) except: profile = models.Profile(user=user) if request.method == 'POST': profile_form = forms.ProfileForm(request.POST, instance=profile) if profile_form.is_valid(): messages.add_message(request, messages.SUCCESS, 'Your profile was stored!') profile_form.save() return HttpResponseRedirect('/userinfo') else: messages.add_message( request, messages.INFO, 'If you want to update your profile, you should fill all the blanks.' ) else: profile_form = forms.ProfileForm() template = get_template('userinfo.html') html = template.render(context=locals(), request=request) response = HttpResponse(html) return response
def userinfo(request): if request.user.is_authenticated: # print(request.user.is_authenticated) username = request.user.username user = User.objects.get(username=username) try: # print(user, 'user') profile = models.Profile.objects.get(user=user) # userinfo = models.Profile.objects.get(user=user) # print(userinfo, 'userinfo') except: profile = models.Profile(user=user) # if 'user_name' in request.session: # username = request.session['user_name'] # else: # return redirect(reverse('login')) # # try: # userinfo = models.User.objects.get(name=username) # except: # pass # template = get_template('userinfo.html') # html = template.render(locals()) # return render(request, 'userinfo.html', locals()) if request.method == 'POST': profile_form = forms.ProfileForm(request.POST, instance=profile) if profile_form.is_valid(): messages.add_message(request, messages.INFO, '个人资料已存储') profile_form.save() return HttpResponseRedirect(reverse('userinfo')) else: messages.add_message(request, messages.INFO, '要修改个人资料,每一个字段都要填...') else: profile_form = forms.ProfileForm() return render(request, 'userinfo.html', locals())
def userinfo(request): if request.user.is_authenticated(): username = request.user.username user = User.objects.get(username=username) try: profile = models.Profile.objects.get(user=user) except: profile = models.Profile(user=user) if request.method == 'POST': profile_form = forms.ProfileForm(request.POST, instance=profile) if profile_form.is_valid(): messages.add_message(request, messages.INFO, "个人资料已保存") profile_form.save() return HttpResponseRedirect('/userinfo') else: messages.add_message(request, messages.INFO, '要修改个人资料,每一个字段都要填...') else: profile_form = forms.ProfileForm() template = get_template('userinfo.html') request_context = RequestContext(request) request_context.push(locals()) html = template.render(request_context) return HttpResponse(html)
def userinfo(request): #login? if request.user.is_authenticated(): #get curent login user info from auth.models User //request.user is User model object. username = request.user.username #get user info from User (not models.User) user = User.objects.get(username=username) try: #read user info from Profile database by user //User<1:1>Profile userinfo = models.Profile.objects.get(user=user) except: #if read empty userinfo #create instance Profile for specific user userinfo = models.Profile(user=user) #send ProfileForm POST? if request.method == 'POST': #WRITE #container [form ProfileForm] save [Profile(userinfo) instance(user-client post)] profile_form = forms.ProfileForm(request.POST, instance=userinfo) if profile_form.is_valid(): messages.add_message(request, messages.INFO, "個人資料已儲存") profile_form.save() return HttpResponseRedirect('/userinfo') else: messages.add_message(request, messages.INFO, '要修改個人資料,每一個欄位都要填...') """ #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 profile_form = forms.ProfileForm() template = get_template('userinfo.html') html = template.render(locals(), request) return HttpResponse(html)
def userinfo(request): if request.user.is_authenticated: username = request.user.username user = User.objects.get(username=username) try: profile = models.Profile.objects.get(user=user) except: profile = models.Profile(user=user) if request.method == 'POST': profile_form = forms.ProfileForm(request.POST, instance=profile) if profile_form.is_valid(): messages.add_message(request, messages.INFO, "个人资料已存储") profile_form.save() return HttpResponseRedirect('/userinfo') else: messages.add_message(request, messages.INFO, '要修改资料,每一项都要填...') else: profile_form = forms.ProfileForm() return render(request, 'userinfo.html', locals())