python - 将 context[] 中的多个 Django 表单传递给模板

标签 python django django-forms django-templates django-views

我有一个自定义 Django 用户架构来管理角色或用户类型:

  • 学生文件
  • 教授简介
  • 高管简介

由于我的 AbstractBaseUser 模型的一部分保持如此:

class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(unique=True)
    username = models.CharField(max_length=40, unique=True)
    slug = models.SlugField(max_length=100, blank=True)
    is_student = models.BooleanField(default=False)
    is_professor = models.BooleanField(default=False)
    is_executive = models.BooleanField(default=False)

    other fields ...

我有 get_student_profile()get_professor_profile()get_executive_profile() 方法来获取个人资料用户数据。

我已经重写了自定义模型 User 中的 save() 方法,以在检查 is_student 或 is_professor 或 is_executive 以创建配置文件时保存个人资料数据用户,他们的数据将保存在各自的模型中:

class StudentProfile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    slug = models.SlugField(max_length=100,blank=True)
    origin_education_school = models.CharField(max_length=128)
    current_education_school = models.CharField(max_length=128)
    extra_occupation = models.CharField(max_length=128)

class ProfessorProfile(models.Model):
    CATHEDRAL_PROFESSOR = 'CATHEDRAL'
    RESEARCH_PROFESSOR = 'RESEARCH'
    INSTITUTIONAL_DIRECTIVE = 'DIRECTIVE'

    OCCUPATION_CHOICES = (
        (CATHEDRAL_PROFESSOR, 'Cathedral Professor'),
        (RESEARCH_PROFESSOR, 'Research Professor'),
        (INSTITUTIONAL_DIRECTIVE, 'Institutional Directive'),
    )
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    slug = models.SlugField(max_length=100,blank=True)
    occupation = models.CharField(max_length=255,blank = False)

class ExecutiveProfile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    slug = models.SlugField(max_length=100, blank=True)
    occupation = models.CharField(max_length=255,blank = False)
    enterprise_name = models.CharField(max_length=255,blank = False)

我有每个配置文件用户的表格

class UserUpdateForm(forms.ModelForm):
    class Meta:
        widgets = {'gender':forms.RadioSelect,}
        fields = ("username", "email", "is_student",           "is_professor", "is_executive",)
        model = get_user_model() #My model User

class StudentProfileForm(forms.ModelForm):
    class Meta:
        model = StudentProfile
        fields = ('origin_education_school',current_education_school',
            'extra_occupation')

class ProfessorProfileForm(forms.ModelForm):
    class Meta:
        model = ProfessorProfile
        fields = ('occupation',)

class ExecutiveProfileForm(forms.ModelForm):
    class Meta:
        model = ExecutiveProfile
        fields = ('occupation', 'enterprise_name', 'culturals_arthistic','ecological')

我有此 URL 可以访问个人资料用户

url(r"^profile/(?P<slug>[\w\-]+)/$",
        views.AccountProfilesView.as_view(
            model=ProfessorProfile),
            name='profile'
    ),

在我的 AccountProfilesView 中,我管理配置文件表单并将其发送到模板

class AccountProfilesView(LoginRequiredMixin, UpdateView):
    # All users can access this view
    model = get_user_model()
    success_url = reverse_lazy('dashboard')
    template_name = 'accounts/profile_form.html'
    fields = '__all__'

    def get_context_data(self, **kwargs):
        context = super(AccountProfilesView, self).get_context_data(**kwargs)
        user = self.request.user

        if not self.request.POST:
            if user.is_student:
                profile = user.get_student_profile()
                context['userprofile'] = profile
                context['form_student'] = forms.StudentProfileForm()
            elif user.is_professor:
                profile = user.get_professor_profile()
                context['userprofile'] = profile
                context['form_professor'] = forms.ProfessorProfileForm()
                print ("profesor form is", context['form_professor'])
            elif user.is_executive:
                profile = user.get_executive_profile()
                context['userprofile'] = profile
                context['form_executive'] = forms.ExecutiveProfileForm()
            elif user.is_student and user.is_professor and user.is_executive:
                student_profile = user.get_student_profile()
                professor_profile = user.get_professor_profile()
                executive_profile = user.get_executive_profile()
                context['student_profile'] = student_profile
                context['professor_profile'] = professor_profile
                context['executive_form'] = executive_profile

                student_form = forms.StudentProfileForm()
                professor_form = forms.ProfessorProfileForm()

                executive_form = forms.ExecutiveProfileForm()

                context['form_student'] = student_form
                context['form_professor'] = professor_form
                context['form_executive'] = executive_form
        return context

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        context = super(AccountProfilesView, self).post(request, *args, **kwargs)
        user = self.request.user
        if user.is_student:
            context['form_student'] = forms.StudentProfileForm(self.request.POST)
        elif user.is_professor:
            context['form_professor'] = forms.ProfessorProfileForm(self.request.POST)
        elif user.is_executive:
            context['form_executive'] = forms.ExecutiveProfileForm(self.request.POST)

        elif user.is_student and user.is_professor and user.is_executive:
            context['form_student'] = forms.StudentProfileForm(self.request.POST)
            context['form_professor'] = forms.ProfessorProfileForm(self.request.POST)
            context['form_executive'] = forms.ExecutiveProfileForm(self.request.POST)

        return context

    def form_valid(self, form):
        context = self.get_context_data(form=form)
        user = self.request.user
        user = form.save()
        if user.is_student:
            student = context['form_student'].save(commit=False)
            student.user = user
            student.save()
        elif user.is_professor:
            professor = context['form_professor'].save(commit=False)
            professor.user = user
            professor.save()
        elif user.is_executive:
            executive = context['form_executive'].save(commit=False)
            executive.user = user
            executive.save()

        elif user.is_student and user.is_professor and user.is_executive:
            student = context['form_student'].save(commit=False)
            student.user = user
            student.save()

            professor = context['form_professor'].save(commit=False)
            professor.user = user
            professor.save()

            executive = context['form_executive'].save(commit=False)
            executive.user = user
            executive.save()
        return super(AccountProfilesView, self).form_valid(form)

发生的情况是,在 get_context_data() 方法中,context['form_professor'] = forms.ProfessorProfileForm()context['form_executive'] = forms.ExecutiveProfileForm() 尚未传递给模板

当我使用学生资料 (is_student) 或教授资料 (is_professor) 或高管资料 (is_executive) 创建用户时,所有工作正常,这意味着,通过分隔的用户配置文件(只有一个配置文件的用户)呈现表单配置文件根据相应的配置文件字段显示表单。

从这个意义上说,在 get_context_data() 部分中,以下代码可以工作并传递相应的表单:

if user.is_student:
    profile = user.get_student_profile()
    context['userprofile'] = profile
    context['form_student'] = forms.StudentProfileForm()
elif user.is_professor:
    profile = user.get_professor_profile()
    context['userprofile'] = profile
    context['form_professor'] = forms.ProfessorProfileForm()
    print ("profesor form is", context['form_professor'])
elif user.is_executive:
    profile = user.get_executive_profile()
    context['userprofile'] = profile
    context['form_executive'] = forms.ExecutiveProfileForm()

但是当我创建一个检查了三个配置文件的用户(is_studentis_professoris_executive)时,只需传递 context[' form_student'] = forms.StudentProfileForm()

我显示数据的模板是:

{% extends 'layout.html' %}
{% load bootstrap3 %}
{% block title_tag %}Accounts | Profile | iHost {{ block.super }}{% endblock %}
{% block body_content %}
<div class="container">
    <h1>My Profile</h1>
    <form method="POST">
        {% csrf_token %}

        # I ask for user profile of a separate way

        # Student Profile User  
        {% if userprofile.user.is_student %}
        <br/><hr>
            Student form
            {% bootstrap_form form_student %}
        {% endif %}

        # Professor Profile User  
        {% if userprofile.user.is_professor %}
            {{ form_professor.as_p }}
        {% endif %}

        # Executive Profile User       
        {% if userprofile.user.is_executive %}  
            {{ form_executive.as_p }}
           {% comment %} {% bootstrap_form form_executive %}{% endcomment %}
        {% endif %}

        # I ask for an user with three profiles

        {% if userprofile.user.is_student  %}
        <br/><hr>
        Student form
            {% if userprofile.user.is_professor %}
                {% if userprofile.user.is_executive %}
                    {% bootstrap_form form_student %}
                    <br/><hr>
                    Professor form does not arrive
                    {{ form_professor.as_p }}
                    {% comment %}  {% bootstrap_form form_professor %}{% endcomment %}
                    <br/><hr>
                    Executive form does not arrive
                    {{ form_executive.as_p }}    
                    {% comment %}
                    {% bootstrap_form form_student %}
                    {% bootstrap_form form_professor %}
                    {% endcomment %}         
                {% endif %}
            {% endif %}
        {% endif %}
<br /><br /><br />

        <input type="submit" value="Save Changes" class="btn btn-default">
    </form>
</div>

{% endblock %}

当我使用具有三个配置文件的用户登录并呈现配置文件表单时,我会得到与呈现的表单相关的此行为。 form_professor 和 form_executive 未渲染

enter image description here

因此,我认为教授和执行表单实例并未传递给模板,

这也得到了验证,因为如果我删除注释并在模板中使用 django-bootstrap3 应用程序,我将无法获取 form_professorform_executive > 实例表单

  • form_professor 未到达模板 enter image description here

  • form_executive 未到达模板 enter image description here

此外,尽管我认为这样我可以更好地执行模板中的条件逻辑。

我这里发生了什么事。 我的想法是可以根据配置文件在一个模板中向用户呈现配置文件表单。

我非常感谢他们的支持。

<小时/> 更新

<小时/>

根据@Rohan下面的答案,是的,el ... if 语句是问题的根源。现在表单呈现良好。

我的AccountProfilesView保持这种方式:

class AccountProfilesView(LoginRequiredMixin, UpdateView):
    # All users can access this view
    model = get_user_model()
    #success_url = reverse_lazy('dashboard')
    template_name = 'accounts/profile_form.html'
    fields = '__all__'

    def get_context_data(self, **kwargs):
        context = super(AccountProfilesView, self).get_context_data(**kwargs)
        user = self.request.user

        if not self.request.POST:
            if user.is_student:
                profile = user.get_student_profile()
                context['userprofile'] = profile
                context['form_student'] = forms.StudentProfileForm()
            if user.is_professor:
                profile = user.get_professor_profile()
                context['userprofile'] = profile
                context['form_professor'] = forms.ProfessorProfileForm()
                print ("profesor form is", context['form_professor'])
            if user.is_executive:
                profile = user.get_executive_profile()
                context['userprofile'] = profile
                context['form_executive'] = forms.ExecutiveProfileForm()
        return context

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        context = super(AccountProfilesView, self).post(request, *args, **kwargs)
        user = self.request.user
        if user.is_student:
            context['form_student'] = forms.StudentProfileForm(
                self.request.POST)
        if user.is_professor:
            context['form_professor'] = forms.ProfessorProfileForm(
                self.request.POST)
        if user.is_executive:
            context['form_executive'] = forms.ExecutiveProfileForm(
                self.request.POST)
        return context

    def form_valid(self, form):
        context = self.get_context_data(form=form)
        user = self.request.user
        user = form.save()
        if user.is_student:
            student = context['form_student'].save(commit=False)
            student.user = user
            student.save()
        if user.is_professor:
            professor = context['form_professor'].save(commit=False)
            professor.user = user
            professor.save()
        if user.is_executive:
            executive = context['form_executive'].save(commit=False)
            executive.user = user
            executive.save()
        return super(AccountProfilesView, self).form_valid(form)

    def get_success_url(self):
        return reverse('dashboard')

但是现在,当我想保存三个配置文件的表单部署产品时,我收到此错误消息。

File "/home/bgarcial/workspace/ihost_project/accounts/views.py", line 181, in post
    context['form_student'] = forms.StudentProfileForm(self.request.POST)
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/http/response.py", line 142, in __setitem__
    value = self._convert_to_charset(value, 'latin-1', mime_encode=True)
  File "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/http/response.py", line 115, in _convert_to_charset
    raise BadHeaderError("Header values can't contain newlines (got %r)" % value)
django.http.response.BadHeaderError: Header values can't contain newlines (got '<tr><th><label for="id_origin_education_school">Origin education institute:</label></th><td><input id="id_origin_education_school" maxlength="128" name="origin_education_school" type="text" value="Universidad CES, Escuela de Ingeniería" required /></td></tr>\n<tr><th><label for="id_current_education_school">Current education institute:</label></th><td><input id="id_current_education_school" maxlength="128" name="current_education_school" type="text" value="Universidad CES" required /></td></tr>\n<tr><th><label for="id_extra_occupation">Extra occupation:</label></th><td><input id="id_extra_occupation" maxlength="128" name="extra_occupation" type="text" value="Networker" required /></td></tr>')
[08/Apr/2017 16:45:05] "POST /accounts/profile/milena/ HTTP/1.1" 500 108439

当要渲染表单时,有一些不方便,并且我确保使用 POST 方法中输入的数据填充此表单,发生错误并得到此屏幕:

enter image description here

最佳答案

get_context_data 函数中的 python 代码存在问题。您不应使用 if..elif.. 添加所需的表单。使用您的代码,如果用户拥有所有个人资料,则该代码将仅进入第一个 if 并仅添加学生个人资料表单。

因此,您应该为所有类型的配置文件使用单独的 if 语句。在这种情况下,您不需要对所有类型使用最后的 ifanding。

因此将您的代码更改为

if user.is_student:
    profile = user.get_student_profile()
    context['userprofile'] = profile
    context['form_student'] = forms.StudentProfileForm()

#NOTE: no elif, only if
if user.is_professor:
    profile = user.get_professor_profile()
    context['userprofile'] = profile
    context['form_professor'] = forms.ProfessorProfileForm()
    print ("profesor form is", context['form_professor'])

if user.is_executive:
    profile = user.get_executive_profile()
    context['userprofile'] = profile
    context['form_executive'] = forms.ExecutiveProfileForm()

# NO need for last if user.is_student and user.is_professor and user.is_executive:

关于python - 将 context[] 中的多个 Django 表单传递给模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43290176/

相关文章:

Python 多处理 : How to close the multiprocessing pool on exception

python - Pandas 根据列值将数据框拆分为多个 csv

python - 将索引拆分为 Pandas 中的单独列

django - Django URL字段的有效值是什么?

python - 如何使用 OpenAI 的 API 使用批量嵌入?

python - Django Channels/Daphne 中的 Websocket 超时

django - 如何通过 django-crispy-forms 中的取消按钮重定向到 url?

django - 如何给 `def formfield_for_manytomany` 一个可编辑对象的 id

python - django - 如何交叉检查 ModelAdmin 及其内联?

DjangoForeignKey表单字段: display value