【问题标题】:django 1.6 automatically remove or add http:// from URLField from form datadjango 1.6 自动从表单数据的 URLField 中删除或添加 http://
【发布时间】:2014-03-22 08:13:02
【问题描述】:

我正在阅读 Tango With Django 教程,我在表单章节 (http://www.tangowithdjango.com/book/chapters/forms.html) 中遇到了一个我无法开始工作的函数。

诚然,我正在学习使用 Python 3.3 和 Django 1.6 的教程,但是到目前为止,我已经能够完成教程。

clean 函数 forms.py 应该清理 URLField:

class PageForm(forms.ModelForm):
    title = forms.CharField(max_length=128, help_text="input page title")
    url = forms.URLField(max_length=200, help_text="input page URL")
    views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)

    def clean(self, cleaned_data):
        cleaned_data = super(PageForm, self).clean()
        url = cleaned_data.get('url')

        if url and not url.startswith('http://'):
            url = 'http://' + url
            cleaned_data['url'] = url

        return cleaned_data

    class Meta:
        model = Page
        fields = ('title', 'url', 'views')

这是add_page.html模板的摘录:

<form id="page_form" method="POST" action="/rango/category/{{category_name_url}}/add_page/">

        {% csrf_token %}
        {% for hidden in form.hidden_fields %}
            {{ hidden }}
        {% endfor %}

        {% for field in form.visible_fields %}
        <p></p>
            {{ field.errors }}
            {{ field.help_text }}
            {{ field }}
        {% endfor %}

        <p></p>
        <input type="submit" name="submit" value="create page" />
        <br>
</form>

作为一种解决方法,我已根据官方 Django 文档调整了 forms.py url 函数以这种方式工作,尽管这不是我的首选方法:

url = forms.URLField(
    max_length=200, help_text="input page URL", initial='http://')

【问题讨论】:

    标签: django-forms django-views django-1.6


    【解决方案1】:

    如果您想确保表单中url字段中的输入值在保存时应该有一个前缀“http://”,无论用户输入与否,此行已经正确:

    url = forms.URLField(max_length=200, help_text="input page URL", initial='http://')
    

    试试这个:

    >>> from django import forms
    >>> url = forms.URLField(initial='http://')
    >>> print url.clean('google.com')
    http://google.com/
    

    无需在表单中实现 clean() 方法。

    更新 1:

    在 PageForm 类中,您必须创建一个 clean_url() 方法来执行您想要的输入,并在 save() 方法中将清理后的数据分配给您要保存的对象。提示:看看我的 django 表单forms.py

    【讨论】:

    • 我想要它做的是添加或删除 http://,这取决于它是否是用户输入的一部分。
    • @user1026169 所以你的意思是当用户在 clean() 方法中输入带有'example.com'的表单时,你将删除'http://'或者当用户输入在 clean() 方法中的“example.com”形式中,您将添加“http://”?重点是什么?您真正想对 url 字段中的值做什么?
    • 如果有人只输入example.com,我希望它变成http://example.com,如果有人输入http://example.com,我希望它保持原样。我不希望 urlfield 中已经包含 http://。
    【解决方案2】:

    我也有这个问题。我的问题是在输入缺少http:// 的url 字符串时不断显示一个弹出窗口,说“请输入一个URL”。所以clean() 电话从来没有机会发生。

    我认为这是因为表单中URLfield 的默认小部件会执行检查。通过执行以下操作,clean() 代码有机会发生并添加最终丢失的“http://”

    from django.forms.widgets import TextInput
    ...
    url = forms.URLField(max_length=200, 
                         help_text="Please enter the URL of the page.", 
                         initial="http://",
                         widget=TextInput)
    

    默认为widget=UrlInput

    【讨论】:

      【解决方案3】:

      我想在这里添加另一个我从丹尼尔的答案here学到的答案

      验证消息“请输入 URL”不是来自 Django。它来自试图在客户端验证它的浏览器(因为我认为输入字段中的type 设置为"url")。为了不让浏览器验证它,只需将novalidate 属性放在表单中:

      <form id="page_form" method="POST" ... novalidate>
      

      【讨论】:

        猜你喜欢
        • 2017-01-31
        • 2013-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多