Django - 两个不同视图(窗体)中的UPDATE用户字段

时间:2018-04-18 17:17:00

标签: python django django-forms django-views django-users

我很难有这么简单的事情(我猜)。

我的目标是创建两个不同形式的子页面,但使用相同的用户模型连接

/account/register.html - 仅限管理注册的页面(使用登录名,电子邮件,密码创建用户) /account/questionnaire.html - 更新相同用户信息的页面,例如年龄,体重,身高等。

我在服务器日志中进行'POST'通信,但是当我检查django管理站点时没有出现。

models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    age = models.PositiveIntegerField(blank=False)
    weight = models.PositiveIntegerField(blank=False)
    height = models.PositiveIntegerField(blank=False)

forms.py

from django import forms
from django.core import validators
from django.contrib.auth.models import User
from account.models import UserProfile

class RegisterUserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta():
        model = User
        fields = ('username','email','password')

class RegisterUserInfoForm(forms.ModelForm):

    class Meta():
        model = UserProfile
        fields = ('age','weight','height')

views.py

from django.shortcuts import render
from account.forms import RegisterUserForm, RegisterUserInfoForm
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.contrib.auth.decorators import login_required

def register(request):
        registered = False

        if request.method == 'POST':
            user_form = RegisterUserForm(data=request.POST)

            if user_form.is_valid():
                user = user_form.save()
                user.set_password(user.password)
                user.save()

                registered = True

            else:
                print(user_form.errors)

        else:
            user_form = RegisterUserForm()

        return render(request,'account/register.html',{
            'user_form':user_form,
            'registered':registered,
        })

@login_required
def questionnaire(request):
    if request.method == 'POST':
        profile_form = RegisterUserInfoForm(request.POST, instance=request.user)
        if profile_form.is_valid():
            profile_form.save()
        else:
            print(profile_form.errors)
    else:
        profile_form = RegisterUserInfoForm(instance=request.user)
    return render(request,'account/questionnaire.html',{
        'profile_form':profile_form,
    })

register.html

{% extends 'base.html' %}

{% block body_block %}
  <div class="container">
    <h1>Register</h1>   
        <form method="post">
        {% csrf_token %}
        {{ user_form.as_p }}
        <input type="submit" name="btn btn-primary" value="Save">
      </form>
  </div>
{% endblock %}

questionnaire.html

{% extends 'base.html' %}

{% block body_block %}
<div class="container">
  <h1>questionnaire</h1>
  <form method="post">
    {% csrf_token %}
    {{ profile_form.as_p }}
    <input type="submit" name="" value="Save">
  </form>
</div>

{% endblock %}

1 个答案:

答案 0 :(得分:0)

您的视图未收到POST请求,因为您没有为表单标记提供操作属性。因此,您的表单无处传递您的POST请求。试试这样:

  <form action="" method="post">
    {% csrf_token %}
    {{ user_form.as_p }}
    <input type="submit" name="btn btn-primary" value="Save">
  </form>

此外,您一定要查看django的内置通用视图:JavaCreateView。它们完全用于此目的,几乎可以为您提供一切。