Django-form表单

构建一个表单

假设你想在你的网站上创建一个简单的表单,以获得用户的名字。你需要类似这样的模板:

Your

这是一个非常简单的表单。实际应用中,一个表单可能包含几十上百个字段,其中大部分需要预填充,而且我们预料到用户将来回编辑-提交几次才能完成操作。

我们可能需要在表单提交之前,在浏览器端作一些验证。我们可能想使用非常复杂的字段,以允许用户做类似从日历中挑选日期这样的事情,等等。

这个时候,让Django 来为我们完成大部分工作是很容易的。

我们已经计划好了我们的 HTML 表单应该呈现的样子。在Django 中,我们的起始点是这里:

Your

注意它不包含
标签和提交按钮。我们必须自己在模板中提供它们。

发送给Django 网站的表单数据通过一个视图处理,一般和发布这个表单的是同一个视图。这允许我们重用一些相同的逻辑。

当处理表单时,我们需要在视图中实例化它:

<span style="color: #0000ff;">from django.shortcuts <span style="color: #0000ff;">import<span style="color: #000000;"> render
<span style="color: #0000ff;">from django.http <span style="color: #0000ff;">import<span style="color: #000000;"> HttpResponseRedirect

<span style="color: #0000ff;">from .forms <span style="color: #0000ff;">import<span style="color: #000000;"> NameForm

<span style="color: #0000ff;">def<span style="color: #000000;"> get_name(request):
<span style="color: #008000;">#<span style="color: #008000;"> if this is a POST request we need to process the form data
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">:
<span style="color: #008000;">#<span style="color: #008000;"> create a form instance and populate it with data from the request:
form =<span style="color: #000000;"> NameForm(request.POST)
<span style="color: #008000;">#<span style="color: #008000;"> check whether it's valid:
<span style="color: #0000ff;">if<span style="color: #000000;"> form.is_valid():
<span style="color: #008000;">#<span style="color: #008000;"> process the data in form.cleaned_data as required
<span style="color: #008000;">#<span style="color: #008000;"> ...
<span style="color: #008000;">#<span style="color: #008000;"> redirect to a new URL:
<span style="color: #0000ff;">return HttpResponseRedirect(<span style="color: #800000;">'<span style="color: #800000;">/thanks/<span style="color: #800000;">'<span style="color: #000000;">)

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; if a GET (or any other method) we'll create a blank form</span>
<span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;:
    form </span>=<span style="color: #000000;"&gt; NameForm()

</span><span style="color: #0000ff;"&gt;return</span> render(request,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;name.html</span><span style="color: #800000;"&gt;'</span>,{<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;form</span><span style="color: #800000;"&gt;'</span>: form})</pre>

如果访问视图的是一个GET请求,它将创建一个空的表单实例并将它放置到要渲染的模板的上下文中。这是我们在第一个访问该URL 时预期发生的情况。

如果表单的提交使用POST请求,那么视图将再次创建一个表单实例并使用请求中的数据填充它:form = NameForm(request.POST)。这叫做”绑定数据至表单“(它现在是一个绑定的表单)。

我们调用表单的is_valid()方法;如果它不为True,我们将带着这个表单返回到模板。这时表单不再为空(未绑定),所以HTML 表单将用之前提交的数据填充,然后可以根据要求编辑并改正它。

如果is_valid()True,我们将能够在cleaned_data属性中找到所有合法的表单数据。在发送HTTP 重定向给浏览器告诉它下一步的去向之前,我们可以用这个数据来更新数据库或者做其它处理。

我们不需要在name.html 模板中做很多工作。最简单的例子是:

根据{{ form }},所有的表单字段和它们的属性将通过Django 的模板语言拆分成HTML 标记 。

注:Django 原生支持一个简单易用的。当提交一个启用CSRF 防护的POST表单时,你必须使用上面例子中的csrf_token模板标签。

现在我们有了一个可以工作的网页表单,它通过Django Form 描述、通过视图处理并渲染成一个HTML

如果我们自定义的验证提示等,当我们在前端页面使用时:

标题: {{ article_form.title }} 内容: </span><span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span> <span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;div</span><span style="color: #0000ff;"&gt;></span> <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;><</span><span style="color: #800000;"&gt;input </span><span style="color: #ff0000;"&gt;type</span><span style="color: #0000ff;"&gt;="submit"</span><span style="color: #ff0000;"&gt; value</span><span style="color: #0000ff;"&gt;="提交"</span><span style="color: #0000ff;"&gt;></</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span> <span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;form</span><span style="color: #0000ff;"&gt;></span></pre>

如果出现以下错误提示时:

(index):1 An invalid form control with name= focusable.

我们需要在加上 novalidate

<div style="text-align: right;"><a href="#_labelTop">

Django Form 类详解

绑定的和未绑定的表单实例

绑定的和未绑定的表单 之间的区别非常重要:

  • 未绑定的表单没有关联的数据。当渲染给用户时,它将为空或包含默认的值。
  • 绑定的表单具有提交的数据,因此可以用来检验数据是否合法。如果渲染一个不合法的绑定的表单,它将包含内联的错误信息,告诉用户如何纠正数据。

考虑一个比上面的迷你示例更有用的一个表单,我们完成一个更加有用的注册表单:

<span style="color: #0000ff;">from django <span style="color: #0000ff;">import<span style="color: #000000;"> forms

<span style="color: #0000ff;">class<span style="color: #000000;"> RegisterForm(forms.Form):
username = forms.CharField(max_length=100<span style="color: #000000;">,error_messages={<span style="color: #800000;">"<span style="color: #800000;">min_length<span style="color: #800000;">":<span style="color: #800000;">"<span style="color: #800000;">最短为5个字符<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">required<span style="color: #800000;">":<span style="color: #800000;">"<span style="color: #800000;">该字段不能为空<span style="color: #800000;">"<span style="color: #000000;">},)
password = forms.CharField(max_length=100<span style="color: #000000;">,widget=widgets.PasswordInput(attrs={<span style="color: #800000;">"<span style="color: #800000;">placeholder<span style="color: #800000;">":<span style="color: #800000;">"<span style="color: #800000;">password<span style="color: #800000;">"<span style="color: #000000;">})
)

telephone</span>=<span style="color: #000000;"&gt;forms.IntegerField()
email </span>=<span style="color: #000000;"&gt; forms.EmailField()
is_married </span>= forms.BooleanField(required=False)</pre>

1= fields.CharField(error_messages={: headmaster_id = fields.ChoiceField(choices=models.UserInfo.objects.filter(ut_id=2).values_list(, </span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; class_add(request): </span><span style="color: #0000ff;"&gt;if</span> request.method == <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;GET</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: form </span>=<span style="color: #000000;"&gt; ClassForm() </span><span style="color: #0000ff;"&gt;return</span> render(request,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;class_add.html</span><span style="color: #800000;"&gt;'</span>,{<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;form</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;:form}) </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;: form </span>= ClassForm(data=<span style="color: #000000;"&gt;request.POST) </span><span style="color: #0000ff;"&gt;if</span> <span style="color: #0000ff;"&gt;not</span><span style="color: #000000;"&gt; form.is_valid(): </span><span style="color: #0000ff;"&gt;return</span> render(request,{<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;form</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: form}) models.ClassList.objects.create(</span>**<span style="color: #000000;"&gt;form.cleaned_data) </span><span style="color: #0000ff;"&gt;return</span> redirect(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;/class_list/</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;) </span>1<span style="color: #000000;"&gt;. headmaster_id </span>2<span style="color: #000000;"&gt;. <span style="color: #ff0000;"&gt;数据源无法实施更新,重写构造方法</span> 方式一(推荐): </span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; ClassForm(Form): caption </span>= fields.CharField(error_messages={<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;required</span><span style="color: #800000;"&gt;'</span>:<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;班级名称不能为空</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;}) </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; headmaster = fields.ChoiceField(choices=[(1,)])</span> headmaster_id = fields.ChoiceField(choices=<span style="color: #000000;"&gt;[]) </span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__init__</span>(self,*args,**<span style="color: #000000;"&gt;kwargs): super().</span><span style="color: #800080;"&gt;__init__</span>(*args,**<span style="color: #000000;"&gt;kwargs) self.fields[</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;headmaster_id</span><span style="color: #800000;"&gt;'</span>].choices = models.UserInfo.objects.filter(ut_id=2).values_list(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;id</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;username</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;) 方式二: </span><span style="color: #0000ff;"&gt;from</span> django.forms.models <span style="color: #0000ff;"&gt;import</span><span style="color: #000000;"&gt; ModelChoiceField </span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; ClassForm(Form): caption </span>= fields.CharField(error_messages={<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;required</span><span style="color: #800000;"&gt;'</span>:<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;班级名称不能为空</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;}) </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; headmaster = fields.ChoiceField(choices=[(1,)])</span> headmaster_id = ModelChoiceField(queryset=models.UserInfo.objects.filter(ut_id=2))</pre>

<p class="line" data-line="260">


<h3 class="line" data-line="260">Widgets

每个表单字段都有一个对应的Widget类,它对应一个HTML 表单Widget,例如

在大部分情况下,字段都具有一个合理的默认Widget。例如,默认情况下,CharField具有一个TextInput Widget,它在HTML 中生成一个

不管表单提交的是什么数据,一旦通过调用is_valid()成功验证(is_valid()返回True),验证后的表单数据将位于form.cleaned_data字典中。这些数据已经为你转换好为Python 的类型。

注:此时,你依然可以从request.POST中直接访问到未验证的数据,但是访问验证后的数据更好一些。

在上面的联系表单示例中,将是一个布尔值。类似地,IntegerFieldFloatField字段分别将值转换为Python 的intfloat

form的字段可以定义正则表达式

from django.core.validators import RegexValidator

password ===3=18=: : : : , =[RegexValidator(,

主动向form中添加错误信息

form.add_error(,ValidationError())

form初始化填写input里的默认值

= models.UserInfo.objects.filter(id=nid,ut_id=1 redirect(</span><span style="color: #0000ff;"&gt;if</span> request.method == <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;GET</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;: </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 显示input,并且将数据库中的默认值填写到input框中</span> form = TeacherForm(initial={<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;username</span><span style="color: #800000;"&gt;'</span>:obj.username,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;password</span><span style="color: #800000;"&gt;'</span>:obj.password,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;email</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;:obj.email}) </span><span style="color: #0000ff;"&gt;return</span> render(request,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;edit_teacher.html</span><span style="color: #800000;"&gt;'</span>,{<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;form</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;:form}) </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;: form </span>= TeacherForm(data=<span style="color: #000000;"&gt;request.POST) </span><span style="color: #0000ff;"&gt;if</span><span style="color: #000000;"&gt; form.is_valid(): </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 校验成功</span> models.UserInfo.objects.filter(id=nid).update(**<span style="color: #000000;"&gt;form.cleaned_data) </span><span style="color: #0000ff;"&gt;return</span> redirect(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;/teachers/</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;) </span><span style="color: #0000ff;"&gt;return</span> render(request,{<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;form</span><span style="color: #800000;"&gt;'</span>:form})</pre>

钩子函数

此处拿登陆验证为例

app01

 from django.core.exceptions import ValidationError


===: : : ===: : </span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; clean_username(self):    <span style="color: #008000;"&gt;#这个就是钩子函数,为哪个字段写,函数名就必须为clean_字段名
    </span></span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; ...form验证username时,若通过了正则验证,则会执行此函数,下面的函数会判断数据库中是否有这个用户名<br />         如果没有则不会再进行其他字段的验证,并会报错提示:用户名不存在<br /></span>
    user = self.cleaned_data[<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;username</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;]
    is_exsit </span>= models.UserInfo.objects.filter(username=<span style="color: #000000;"&gt;user).count()
    </span><span style="color: #0000ff;"&gt;if</span> <span style="color: #0000ff;"&gt;not</span><span style="color: #000000;"&gt; is_exsit:
        </span><span style="color: #0000ff;"&gt;raise</span> ValidationError(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;用户名不存在</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)  
    </span><span style="color: #0000ff;"&gt;return</span> user</pre>

注意:
  必须有返回值
  只能拿自己当前字段值
  raise ValidationError('xxx')

你需要做的就是将表单实例放进模板的上下文。如果你的表单在Context 中叫做form,那么{{ form }}将正确地渲染它的元素。

对于对,还有几个输出选项:

  • {{ form.as_table }}以表格的形式将它们渲染在标签中
  • {{ form.as_p }}将它们渲染在

    标签中

  • {{ form.as_ul }}将它们渲染在
  • 标签中

注意,你必须自己提供

    元素。

    {{ form.as_p }}会渲染如下:

    Username:
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span>
        <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;label </span><span style="color: #ff0000;"&gt;for</span><span style="color: #0000ff;"&gt;="id_password"</span><span style="color: #0000ff;"&gt;></span>Password:<span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;label</span><span style="color: #0000ff;"&gt;></span>
        <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;input </span><span style="color: #ff0000;"&gt;id</span><span style="color: #0000ff;"&gt;="id_password"</span><span style="color: #ff0000;"&gt; maxlength</span><span style="color: #0000ff;"&gt;="100"</span><span style="color: #ff0000;"&gt; name</span><span style="color: #0000ff;"&gt;="password"</span><span style="color: #ff0000;"&gt; placeholder</span><span style="color: #0000ff;"&gt;="password"</span><span style="color: #ff0000;"&gt; type</span><span style="color: #0000ff;"&gt;="password"</span><span style="color: #ff0000;"&gt; required</span><span style="color: #0000ff;"&gt;=""</span><span style="color: #0000ff;"&gt;></span>
    <span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span>
    
    
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span>
        <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;label </span><span style="color: #ff0000;"&gt;for</span><span style="color: #0000ff;"&gt;="id_telephone"</span><span style="color: #0000ff;"&gt;></span>Telephone:<span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;label</span><span style="color: #0000ff;"&gt;></span> <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;input </span><span style="color: #ff0000;"&gt;id</span><span style="color: #0000ff;"&gt;="id_telephone"</span><span style="color: #ff0000;"&gt; name</span><span style="color: #0000ff;"&gt;="telephone"</span><span style="color: #ff0000;"&gt; type</span><span style="color: #0000ff;"&gt;="number"</span><span style="color: #ff0000;"&gt; required</span><span style="color: #0000ff;"&gt;=""</span><span style="color: #0000ff;"&gt;></span>
    <span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span>
    
    
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span>
        <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;label </span><span style="color: #ff0000;"&gt;for</span><span style="color: #0000ff;"&gt;="id_email"</span><span style="color: #0000ff;"&gt;></span>Email:<span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;label</span><span style="color: #0000ff;"&gt;></span> <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;input </span><span style="color: #ff0000;"&gt;id</span><span style="color: #0000ff;"&gt;="id_email"</span><span style="color: #ff0000;"&gt; name</span><span style="color: #0000ff;"&gt;="email"</span><span style="color: #ff0000;"&gt; type</span><span style="color: #0000ff;"&gt;="email"</span><span style="color: #ff0000;"&gt; required</span><span style="color: #0000ff;"&gt;=""</span><span style="color: #0000ff;"&gt;></span>
    <span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span>
    
    
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span>
        <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;label </span><span style="color: #ff0000;"&gt;for</span><span style="color: #0000ff;"&gt;="id_is_married"</span><span style="color: #0000ff;"&gt;></span>Is married:<span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;label</span><span style="color: #0000ff;"&gt;></span> <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;input </span><span style="color: #ff0000;"&gt;id</span><span style="color: #0000ff;"&gt;="id_is_married"</span><span style="color: #ff0000;"&gt; name</span><span style="color: #0000ff;"&gt;="is_married"</span><span style="color: #ff0000;"&gt; type</span><span style="color: #0000ff;"&gt;="checkbox"</span><span style="color: #0000ff;"&gt;></span>
    <span style="color: #0000ff;"&gt;</</span><span style="color: #800000;"&gt;p</span><span style="color: #0000ff;"&gt;></span>
    
    
    <span style="color: #0000ff;"&gt;<</span><span style="color: #800000;"&gt;input </span><span style="color: #ff0000;"&gt;type</span><span style="color: #0000ff;"&gt;="submit"</span><span style="color: #ff0000;"&gt; value</span><span style="color: #0000ff;"&gt;="注册"</span><span style="color: #0000ff;"&gt;></span>

    <span style="color: #0000ff;"></<span style="color: #800000;">form<span style="color: #0000ff;">>

    <div class="cnblogs_Highlighter sh-gutter">


<h2 class="line" data-line="338">手工渲染字段

我们没有必要非要让Django 来分拆表单的字段;如果我们喜欢,我们可以手工来做(例如,这样允许重新对字段排序)。每个字段都是表单的一个属性,可以使用{{ form.name_of_field }}访问,并将在Django 模板中正确地渲染。例如:

1、

2、

使用{{ form.name_of_field.errors }}显示表单错误的一个清单,并渲染成一个ul。看上去可能像:

Sender is required.

Form表单例子

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


注:所有源代码均实测运行过。所有源代码均已上传CSDN,请有需要的朋友自行下载。
继承APIView和ViewSetMixin;作用也与APIView基本类似,提供了身份认证、权限校验、流量管理等。ViewSet在开发接口中不经常用。
一、Django介绍Python下有许多款不同的 Web 框架。Django是重量级选手中最有代表性的一位。许多成功的网站和APP都基于Django。Django 是一个开放源代码的 Web 应用框架,由 Python 写成。Django 遵守 BSD 版权,初次发布于 2005 年 7 月, 并于 2008 年 9 月发布了第一个正式版本 1.0 。Django学习线路Django 采用了 MVT 的软件设计模式,即模型(Model),视图(View)和模板(Template)。这个MVT模式并
本文从nginx快速掌握到使用,gunicorn快速掌握到使用,实现小白快速搭建django项目,并对可能出现的报错进行了分析
uniapp微信小程序订阅消息发送服务通知
Django终端打印SQL语句 1 Setting配置: 2 默认python 使用的MysqlDB连接,Python3 支持支持pymysql 所有需要在app里面的__init__加上下面配置:
url: re_path(&#39;authors/$&#39;, views.AuthorView.as_view()), re_path(&#39;book/(?P\d+)/$&#39;, vie
前提 关于html寻找路线: template 如果在各个APP中存在, Django 会优先找全局template 文件下的html文件,如果全局下的template文件没有相关的html Djan
// GET请求request.GET // POST请求request.POST // 处理文件上传请求request.FILES // 处理如checkbox等多选 接受列表request.get
from bs4 import BeautifulSoup#kindeditordef kindeditor(request): s = &#39;&#39;&#39; &lt;li&gt;&lt;s
view.py 配置 html 配置
from django.http import JsonResponse JsonResponse 里面代码会加这一个响应头 kwargs.setdefault(&#39;content_type&#
#下面两种是基于QuerySet查询 也就是说SQL中用的jion连表的方式查询books = models.UserInfo.objects.all() print(type(books)) &gt
return HttpResponse(&quot;OK&quot;) 返回一个字符串 return redirect(&quot;/index/&quot;) 返回URL return render
from django.http import JsonResponse JsonResponse 里面代码会加这一个响应头 kwargs.setdefault(&#39;content_type&#
浏览器有一个很重要的概念——同源策略(Same-Origin Policy)。所谓同源是指,域名,协议,端口相同。不同源的客户端脚本(javascript、ActionScript)在没明确授权的情况
自动发送 &gt; 依赖jQuery文件 实例--&gt;GET请求: 手动发送 &gt; 依赖浏览器XML对象(也叫原生ajax) Ajax主要就是使用 【XmlHttpRequest】对象来完成请
#下面两种是基于QuerySet查询 也就是说SQL中用的jion连表的方式查询books = models.UserInfo.objects.all() print(type(books)) &gt
// GET请求request.GET // POST请求request.POST // 处理文件上传请求request.FILES // 处理如checkbox等多选 接受列表request.get
return HttpResponse(&quot;OK&quot;) 返回一个字符串 return redirect(&quot;/index/&quot;) 返回URL return render