Django's ModelForm and Form comparison essay

Display Chinese with ModelForm and Form respectively

ModelForm

Create a table in the model file,

class UserProfile(models.Model):
    user = models.OneToOneField(User,unique=True)
    birth = models.DateField(blank=True,null=True,verbose_name='date of birth')   
    phone = models.CharField(max_length=20,null=True)

Use ModelForm in form file

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('birth','phone')

If you want to display Chinese on the front end, there are 2 ways, 

1 Use verbose_name in the model, then write profile.birth.label_tag in html

 2. Write directly in Chinese
       <div class="form-group">
                <label for="{{ profile.birth.id_for_label }}" class="col-md-5 control-label">{{ profile.birth.label_tag }}</label>  # 1
                <div class="col-md-6 text-left">{{ profile.birth }}</div>
            </div>
            <div class="form-group">
                <label for="{{ profile.phone.id_for_label }}" class="col-md-5 control-label">电话号码</label>    # 2
                <div class="col-md-6 text-left">{{ profile.phone }}</div>     
            </div>

The result is this

Form

write in the form file

class LoginForm(forms.Form):
    username = forms.CharField(label='用户名',max_length=20)
    password = forms.CharField(widget=forms.PasswordInput)

Write form.username.label_tag in the html file to display Chinese

    <form class="form-horizontal" action="." method="post">        {% csrf_token %}
        <div class="form-group">
            <label for="{{ form.username.id_for_label }}" class="col-md-5 control-label" style="color:red"><span class="glyphicon glyphicon-user"></span>{{ form.username.label_tag }}</label>
            <div class="col-md-6 text-left">{{ form.username }}</div>
        </div>
        <div class="form-group">
            <label for="{{ form.password.id_for_label }}" class="col-md-5 control-label" style="color:red"><span class="glyphicon glyphicon-floppy-open"></span>{{ form.password.label_tag }}</label>
            <div class="col-md-6 text-left">{{ form.password }}</div>
        </div>
        <input type="submit" class="btn btn-primary btn-lg" value="登录"/>
    </form>

ps adds a situation: if your view function is not written by yourself but is built-in, how to modify it into Chinese?

For example, for password reset, when sending an email, the email field of the source code is written in English. Modifying the source code is definitely not good, then rewrite a PasswordResetForm class.

url.py

from django.contrib.auth import views as auth_views
from account.forms import PasswordResetForm

urlpatterns = [ url(r'password_reset/$',auth_views.password_reset,{'password_reset_form':PasswordResetForm,'template_name':'account/password_reset_form.html',}),
]

account/forms.py overrides the PasswordResetForm class.

from django.contrib.auth import forms as auth_forms
class PasswordResetForm(auth_forms.PasswordResetForm):
    email = forms.EmailField(label="邮箱", max_length=254)

Then you can write it in html to display Chinese

            <div class="form-group">
                <label class="col-md-5 control-label">{{ form.email.label_tag }}</label>
                <div class="col-md-6 text-left">{{ form.email }}</div>
            </div>

as follows:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325570529&siteId=291194637