$ ls ~yifei/notes/

django forms

Posted on:

Last modified:

django 中的 form 和 model 的用法很像,都是定义一个类,然后指定一些字段就可以了

最简单的 form

from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    email = forms.EmailField(required=False)
    message = forms.CharField(widget=forms.Textarea)

    def clean_message(self):
        message = self.cleaned_data['message']
        num_words = len(message.split())
        if num_words < 4:
            raise forms.ValidationError("Not enough words!")
        return message
def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email', 'noreply@example.com'),
                ['siteowner@example.com'],
            )
            return HttpResponseRedirect('/contact/thanks/')
    else:
        form = ContactForm()
        return render(request, 'contact_form.html', {'form': form})
<form action="" method="post">
    <table>
        {{ form.as_table }}
    </table>
    {% csrf_token %}
    <input type="submit" value="Submit">
</form>
方法用法
form.__str__()return table representation
form.as_p()return p representation
form.as_li()return li representation
form.__getitem__()return element tag
form.__init__(dict)fill values
form.is_bound
form.is_valid()
form.cleaned_data

Note not include table/ul/form tags, just the inside tags

ajax

ajax 中如何指定 crsf token

axios 中:

import axios from 'axios';
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN";
axios.defaults.xsrfCookieName = "csrftoken";

settings.py 中

CSRF_COOKIE_NAME = "csrftoken"

参考

https://stackoverflow.com/questions/39254562/csrf-with-django-reactredux-using-axios

WeChat Qr Code

© 2016-2022 Yifei Kong. Powered by ynotes

All contents are under the CC-BY-NC-SA license, if not otherwise specified.

Opinions expressed here are solely my own and do not express the views or opinions of my employer.

友情链接: MySQL 教程站