Html Django项目-不在浏览器中呈现crispy表单
作为Django项目的一部分,我在views.py文件中创建了以下内容Html Django项目-不在浏览器中呈现crispy表单,html,django,forms,rendering,Html,Django,Forms,Rendering,作为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 "soci
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 %}
运行服务器时没有错误,因此我发现很难排除故障
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形的渲染,以及解决方案(指出我的错误)。我知道u-form是UserUpdateForm的一个实例,但除此之外没有其他内容。context={
“u形”:u形,
“p形式”:p_形式
}
你只是打错了。将-更改为将在一分钟内尝试此操作。为什么它没有出现错误!?因为渲染器不会因未定义的变量而崩溃。
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']