- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 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://')
最佳答案
我也有这个问题。我的问题是,当输入缺少 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
关于django-forms - django 1.6 自动删除或添加 http ://from URLField from form data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923838/
Django 的 django.db.models.URLField使用django.core.validators.URLValidator : class URLField(CharField):
有谁知道如何去掉 Django urlfield 中的“http://”前缀。 我的意思是,当我们将一个字段定义为 urlfield 并尝试向其中输入 url 时,如果没有提供 schema,djan
如何使用自定义验证覆盖 Django 的 URLField 验证?应该在哪里完成? 我希望它也能接受没有域名结尾的 url。 最佳答案 这是 Django 的 Url Field Validator
我正在尝试验证表单。在验证 url 字段时,我需要确保用户提供的字符串没有 http(s):// 但可以使用www. 以及诸如xyz.abc 的字符串.com - 注意两个点。 我试过用 UIAppl
我想保存来自 youtube 的视频的 url 字段,以便我可以将其加载到我的站点中。到目前为止,我在 html 中想到了这个: {{video.title}} 但问题是它只能以一种方式工作。例如:
是否可以使 django (v1.2) 的 URLField 输出一个 HTML5 input 标签,其中 type="url"? ------------ 解决方案------------ from
我现在可以加载 youtube 的单个视频 URL。但是现在的问题是加载youtube的播放列表视频。所以我的问题是,如何替换两个相同的模式,但两者都有不同的 url 替换? 例如: 实际网址: 将
我正在通过示例书 django2 学习 django,并且我正在构建一个社交媒体网站,用户可以在网站上共享图像。完成了一个功能,它使用书签工具(javascript)从其他网站加载图像,然后上传到我的
我正在使用 Django 2.0 并尝试允许用户在 django ModelForm 中输入指向其网站的链接。我希望我的 URLField 能够验证 url,即使它们没有方案前缀。 我已阅读此解决方案
我的申请表中有一个 URLField,我想检查给定的 URL 是否来自 www.domain.com。 现在我正在该领域的清洁方法中使用这个解决方案,但我认为这不是最好的... def cle
我需要一组字符串的精确定义,这些字符串被视为 models.URLField 的合法 URL。 . 就我而言,精确的定义可以是正则表达式,也可以是验证 URL 有效性的一段代码,或者任何其他形式的正式
据我了解,当您使用 PostgreSQL 数据库时,您应该始终使用 TextField 作为可变长度字符串,因为 TextField 和 之间的速度差异CharField 在 PostgreSQL 中
有没有办法使用模板中模型的 URLField 链接到外部站点?我将 href 链接设置为模板变量,但该链接抛出 404 错误。 这是我的模型.py: class Blog(models.Model):
我看到了很多关于这个的问题,但还没有找到答案。 这是我的模型: class UserProfile(models.Model): user = models.OneToOneField(Use
我喜欢 Django 并经常使用它。我发现它的大部分默认设置都是合理的,但有一个一直困扰着我,以至于我在每个项目中都覆盖了它。 URLField 的默认最大长度模型字段为 200 个字符。文档验证了这
我是 Django 的新手,我正在构建一个以表格格式显示模型信息的网页。我为自己发现了一个非常有用的库 - django-tables2,它使构建 html 表变得非常简单。 但是,我无法弄清楚如何自
link = models.URLField(max_length=500, blank=True, default='') 我已经将 max_length 设置为 500,但是每次我尝试使用大于 2
在 Django 中(我们目前使用的是 1.9),当我们将 UrlField 添加到模型时,管理站点会正确地将 UrlField 值呈现为编辑 View 上的可点击链接。 如果我们要将此 UrlFie
如何在验证 URLField 之前删除空格? 使用“clean_[fieldname]()”似乎是来自 https://docs.djangoproject.com/en/dev/ref/forms/
我正在阅读 Tango With Django 教程,我在表单章节 ( http://www.tangowithdjango.com/book/chapters/forms.html ) 中遇到了一个
我是一名优秀的程序员,十分优秀!