【问题标题】:Django: Initial Value from viewDjango:视图中的初始值
【发布时间】:2011-06-28 22:27:38
【问题描述】:

如何从视图中获取初始值?我应该在表单中使用什么参数?

views.py

def cadastro_usuario(request):
    if request.method == 'POST':
        form = cadastroForm(request.POST)
        if form.is_valid():            
            new_user = form.save()
            return HttpResponseRedirect("/")
    else:
        form = cadastroForm()
    return render_to_response("registration/registration.html", {
        'form': form, 'tipo_cadastro': 'PF',})

forms.py

class cadastroForm(UserCreationForm):

    tipo_cadastro = forms.CharField(XXXXX)

【问题讨论】:

  • 从视图中获取什么初始值?
  • 实际上我需要在视图中硬编码一些变量并将它们保存在 UserProfile 中(正如我向 J. Landgrave 解释的那样)

标签: django django-forms django-views


【解决方案1】:

好的,基于您对@J 的回复。 Lnadgrave,假设您的 UserProfile 模型上有一个“user_type”属性,可以设置为“普通”用户或“公司”用户...

#your_app.constants
NORMAL_USER = 0
COMPANY_USER = 1

USER_TYPE_CHOICES = (
    (NORMAL_USER, "Normal"),
    (COMPANY_USER, "Company"),
)


#your_app.models
from django.contrib.auth.models import User
from your_app.constants import USER_TYPE_CHOICES

class UserProfile(models.Model):
    user = models.OneToOne(User)
    user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)


#your_app.forms
from your_app.models import UserProfile

class UserProfileForm(forms.ModelForm):
    class Meta():
        model = UserProfile

    user_type = forms.IntegerField(widget=forms.HiddenInput)


#your_app.views
form django.http import HttpResponseRedirect
from django.shortcuts import render
from your_app.constants import NORMAL_USER, COMPANY_USER
from your_app.forms import UserProfileForm
from your_app.models import UserProfile

def normal_user_registration(request):
    user_profile_form = UserProfileForm(request.POST or None,
        initial={'user_type' : NORMAL_USER})
    if request.method == 'POST':
        user_profile_form.save()
        return HttpResponseRedirect('/')
    return render(request, 'registration/registration.html',
        {'user_profile_form' : user_profile_form})

def company_user_registration(request):
    user_profile_form = UserProfileForm(request.POST or None,
        initial={'user_type' : COMPANY_USER})
    if request.method == 'POST':
        user_profile_form.save()
        return HttpResponseRedirect('/')
    return render(request, 'registration/registration.html',
        {'user_profile_form' : user_profile_form})

这是一种相当冗长的处理方法,但我认为它很清楚如何将初始值传递给您的表单。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我认为您不应该初始化视图中的任何数据,因为它更适合操作请求的数据,例如对普通请求进行操作或设置查询。我认为您正在寻找这个tidbit on initializing values for a form,因为这是我在查看您提供给我们的示例代码后的假设。如果我错了,请纠正我。

    【讨论】:

    • 实际上,我需要为两个不同的视图使用相同的表单和模型。每个视图中只有一个值会改变。抱歉,让我解释一下,我的网站上有两种类型的用户:“普通用户”和“公司用户”都将使用相同的表单和模型进行注册,但在前端会有 2 个不同的注册链接。在模型中,我有一个字段将作为用户类型填充...所以...我想只更改一个变量就可以进行 2 个视图。收到了吗?
    【解决方案3】:

    不确定您到底需要什么,但在我看来,查看Dynamic initial values 可能有用。您可以像这样在视图中为表单定义一个初始值:

    f = cadastroForm(initial={'tipo_cadastro': 'Initial Value!'})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-19
      • 2022-01-13
      • 1970-01-01
      • 2011-12-13
      • 2019-08-11
      • 2011-05-20
      • 2013-02-22
      • 1970-01-01
      相关资源
      最近更新 更多