编辑默认字段' Django中形式的help_text(1.11)

时间:2017-07-13 18:26:02

标签: django django-models django-forms django-views django-users

在过去的几天里,我一直在尝试用我创建的用于处理用户注册的表单上的默认help_text解决一个奇怪的问题。当我看到html django正在插入时,我首先注意到这个问题,因为默认的help_text正在被转义。

Screenshot of Issue

而不是显示<ul>,我提醒你这是django为密码字段包含的默认help_text,它显示纯文本。

所以这是我第一次注意到必须做错事的地方。如果默认表单help_text正在逃脱并且看起来很糟糕,我显然犯了一个错误。接下来,我将解释我尝试解决此问题的方法,然后概述modelformviewtemplate,以便您拥有一些内容从中工作。

我在网上找到的第一个解决方案是使用Meta课程,所以我在我正在修改的forms.py下的class SignUpForm:中进行了操作。

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

class SignUpForm(UserCreationForm):
        company = forms.CharField()
        city = forms.CharField()
        state = forms.CharField()
        zip = forms.IntegerField()
        address = forms.CharField()
        phone = forms.IntegerField()

        class Meta:
            model = User
            # help_text = mark_safe
            fields = ('company', 'city', 'state', 'zip', 'address', 'phone', 'username', 'email', 'password1', 'password2')
            labels = {
                'state': 'US States',
                'password1': 'passcode1',
                'password2': 'passcode2',
                'username': 'human person',
                'email': 'telegraph',
                'city': 'locality',
                'phone': "tele",
            }
            help_texts = {
                'password1': 'Something that doesnt look awful',
                'password2': 'Something else',
                'username': 'Please enter an appropriate human name.',
                'email': 'Which office?',
                'city': 'What county?',
                'phone': 'Please Include Country Code',
            }

这是我开始意识到问题比我想象的要大的地方。不仅导致help_text被转义的东西,其中一些字段接受我的更改而其他字段不接受。我使用默认UserCreationForm扩展的自定义字段(在此示例中为cityphone不会显示新的labelhelp_text,默认字段usernameemail都显示其新的labelhelp_text。并且最重要的是password1password2字段保持不变。

Screenshot of class Meta result

好吧,所以没有用。把它硬编码到表格中怎么样?事实证明,这主要是有效的,但在这个例子中它给我带来了另一层次的复杂性,以及感觉就像不好的做法。我会解释一下。

由于我的表单正在扩展默认django UserCreationForm我实际上并未设置SignUpForm中的字段,因此会自动添加这些字段,并使用class Meta:中的字段所以为了硬编码解决这个问题,我必须添加它们。

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.utils.safestring import mark_safe

class SignUpForm(UserCreationForm):
    username = forms.CharField(help_text=mark_safe("Please enter an appropriate human name."), label='human name')
    email = forms.CharField(widget=forms.EmailInput, help_text=mark_safe('Which office?'), label='telegraph')
    password1 = forms.CharField(widget=forms.PasswordInput, help_text=mark_safe('Something that doesnt look awful'),
                                label='Passcode')
    password2 = forms.CharField(widget=forms.PasswordInput, help_text=mark_safe('Something else'), label='Passcode 2')
    company = forms.CharField(help_text=mark_safe("Please enter a company name"))
    city = forms.CharField(help_text=mark_safe('What county?'), label='locality')
    state = forms.CharField(help_text=mark_safe('Please enter the state'))
    zip = forms.IntegerField(help_text=mark_safe('Please enter a zip code.'))
    address = forms.CharField(help_text=mark_safe('Please enter an address.'))
    phone = forms.IntegerField(help_text=mark_safe('Please include country code.'), label='tele')

    class Meta:
        model = User
        fields = ('company', 'city', 'state', 'zip', 'address', 'phone', 'username', 'email', 'password1', 'password2')

所以这个有效,但是因为我还没有解决根本问题,所以它真的很不切实际。

硬编码结果的屏幕截图(不能发帖,因为我没有足够的代表,但相信我一切正常)

所以这就把我们带到现在,我已经尝试过其他一些事情,但没有什么能让我接近我想要的硬编码,所以我需要找出我所犯的潜在错误。

以下是我正在使用的内容:

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    username = models.OneToOneField(User, on_delete=models.CASCADE)
    company = models.TextField(max_length=500, blank=True)
    city = models.CharField(max_length=100, blank=True)
    state = models.CharField(max_length=100, blank=True)
    zip = models.CharField(max_length=5, blank=True)
    address = models.CharField(max_length=200, blank=True)
    phone = models.CharField(max_length=12, blank=True)

@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    instance.profile.save()

forms.py (当前硬编码版本):

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.utils.safestring import mark_safe

class SignUpForm(UserCreationForm):
    username = forms.CharField(help_text=mark_safe("Please enter an appropriate human name."), label='human name')
    email = forms.CharField(widget=forms.EmailInput, help_text=mark_safe('Which office?'), label='telegraph')
    password1 = forms.CharField(widget=forms.PasswordInput, help_text=mark_safe('Something that doesnt look awful'),
                                label='Passcode')
    password2 = forms.CharField(widget=forms.PasswordInput, help_text=mark_safe('Something else'), label='Passcode 2')
    company = forms.CharField(help_text=mark_safe("Please enter a company name"))
    city = forms.CharField(help_text=mark_safe('What county?'), label='locality')
    state = forms.CharField(help_text=mark_safe('Please enter the state'))
    zip = forms.IntegerField(help_text=mark_safe('Please enter a zip code.'))
    address = forms.CharField(help_text=mark_safe('Please enter an address.'))
    phone = forms.IntegerField(help_text=mark_safe('Please include country code.'), label='tele')

    class Meta:
        model = User
        fields = ('company', 'city', 'state', 'zip', 'address', 'phone', 'username', 'email', 'password1', 'password2')

views.py

from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from apps.dashboard.forms import SignUpForm

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.refresh_from_db()  # load the profile instance created by the signal
            user.profile.company = form.cleaned_data.get('company')
            user.profile.city = form.cleaned_data.get('city')
            user.profile.state = form.cleaned_data.get('state')
            user.profile.zip = form.cleaned_data.get('zip')
            user.profile.address = form.cleaned_data.get('address')
            user.profile.phone = form.cleaned_data.get('phone')
            user.save()
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=user.username, password=raw_password)
            login(request, user)
            return redirect(main)
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})

模板(html):

<h2>Sign up</h2>
                    <form method="post">
                        {% csrf_token %}
                        {% for field in form %}
                            <p>
                                {{ field.label_tag }}<br>
                                {{ field }}
                                {% if field.help_text %}
                                    <small style="color: grey">{{ field.help_text }}</small>
                                {% endif %}
                                {% for error in field.errors %}
                                    <p style="color: red">{{ error }}</p>
                                {% endfor %}
                            </p>
                        {% endfor %}
                        <button type="submit">Sign up</button>
                    </form>

2 个答案:

答案 0 :(得分:3)

回答OP对被转义的help_text的原始担忧:

可以使用'安全'过滤器,例如......

    {% if field.help_text %}
      <small style="color: grey">{{ field.help_text|safe }}</small>
    {% endif %}

提供您在渲染模板中寻找的列表。

您可能希望查看标题为Django template escaping的SO帖子,以获取此行为的更多示例以及如何控制它。

答案 1 :(得分:2)

看起来help_texts仅适用于usernameemail等模型字段。对于其他字段,您可以在help_text方法中设置__init__

class SignUpForm(UserCreationForm):
    class Meta:
        model = User
        ...
        help_texts = {
            'username': 'Please enter an appropriate human name.',
            'email': 'Which office?',
        }

    def __init__(self, *args, **kwargs):
        super(SignUpForm, self).__init__(*args, **kwargs)
        self.fields['password1'].help_text = 'Something that doesnt look awful'
        self.fields['password2'].help_text = 'Something else'

我没有将个人资料字段添加到SignUpForm,而是为个人资料创建一个单独的模型表单

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        ...

然后在视图和模板中包含这两种形式。