【问题标题】:Django project - crispy forms not rendering in the browserDjango 项目 - 没有在浏览器中呈现的脆表单
【发布时间】:2019-05-08 20:47:24
【问题描述】:

作为 Django 项目的一部分,我在 views.py 文件中创建了以下内容

def profile(request):
    u_form =UserUpdateForm()
    p_form =ProfileUpdateForm()

    context={
        'u-form': u_form,
        'p-form': p_form
    }

我现在尝试使用以下代码在 html 页面 (profile.html) 上呈现这些表单:

{% extends "socialmedia/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
      <div class="media">
        <img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
        <div class="media-body">
          <h2 class="account-heading">{{ user.username }}</h2>
          <p class="text-secondary">{{ user.email }}</p>
        </div>
      </div>
     <form method="POST" enctype="multipart/form-data>
        {% csrf_token %}
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">Profile Information</legend>
            {{u_form|crispy}}
            {{p_form|crispy}}
        </fieldset>
        <div class="form-group">
            <button class="btn btn-outline-info" type="submit">Update....</button>
        </div>
    </form>
   </div>
{% endblock content %}

页面上的所有内容都正确呈现,除了这一点:

            {{u_form|crispy}}
            {{p_form|crispy}}

运行服务器时没有错误,所以我发现很难排除故障。

forms.py文件中的代码如下:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile

class UserRegisterForm(UserCreationForm): #form that inherits from the usercreationform
    email = forms.EmailField()

    class Meta: 
        model = User 
        #when this form validates it creates a new user
        #type the fields to be shown on your form, in that order.
        fields = ['username','email','password1','password2']   
"""this gives us a nested name space for configurations and 
        keeps the configs in one place. The model that will be affected is 
        the user model e.g. when we do a form.save it saves it to the user model. 
        And the fields we have are the fields we want on the form. It shows order too. 
        """
#create a model form...this allows us to create a form that#works with a specific database model#
#we want a form that works with our user model
class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField()

    class Meta: 
        model = User 
        fields = ['username','email']

class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model= Profile
        fields=['image']

我的问题是:

谁能告诉我为什么这些附加字段(用户名、电子邮件和图像更新)没有显示在“更新”按钮上方的个人资料 html 中?我在哪个文件中犯了错误。注意:我也很欣赏这些 u-forms 渲染的解释,以及解决方案(指出我的错误)。我知道 u-form 是 UserUpdateForm 的一个实例,但仅此而已。

【问题讨论】:

    标签: html django forms rendering


    【解决方案1】:

    上下文={ 'u-form': u_form, 'p-form': p_form }

    你只是有一个错字。将 - 更改为 _

    【讨论】:

    • 马上试试这个。怎么没有报错!?
    • 因为渲染器不会因未定义的变量而崩溃。
    猜你喜欢
    • 2020-04-28
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2016-01-14
    • 2023-03-12
    相关资源
    最近更新 更多