国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

django模板視圖,表單視圖,各種視圖

2019-11-14 17:17:09
字體:
來源:轉載
供稿:網友

Generic editing views

The following views are described on this page and PRovide a foundation for editing content:

Note

Some of the examples on this page assume that an Author model has been defined as follows inmyapp/models.py:

from django.core.urlresolvers import reversefrom django.db import modelsclass Author(models.Model):    name = models.CharField(max_length=200)    def get_absolute_url(self):        return reverse('author-detail', kwargs={'pk': self.pk})

FormView

class django.views.generic.edit.FormView

A view that displays a form. On error, redisplays the form with validation errors; on success, redirects to a new URL.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

Example myapp/forms.py:

from django import formsclass ContactForm(forms.Form):    name = forms.CharField()    message = forms.CharField(widget=forms.Textarea)    def send_email(self):        # send email using the self.cleaned_data dictionary        pass

Example myapp/views.py:

from myapp.forms import ContactFormfrom django.views.generic.edit import FormViewclass ContactView(FormView):    template_name = 'contact.html'    form_class = ContactForm    success_url = '/thanks/'    def form_valid(self, form):        # This method is called when valid form data has been POSTed.        # It should return an HttpResponse.        form.send_email()        return super(ContactView, self).form_valid(form)

Example myapp/contact.html:

<form action="" method="post">{% csrf_token %}    {{ form.as_p }}    <input type="submit" value="Send message" /></form>

CreateView

class django.views.generic.edit.CreateView

A view that displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

Attributes

template_name_suffix

The CreateView page displayed to a GET request uses a template_name_suffix of '_form'. For example, changing this attribute to '_create_form' for a view creating objects for the example Author model would cause the default template_name to be 'myapp/author_create_form.html'.

object

When using CreateView you have access to self.object, which is the object being created. If the object hasn’t been created yet, the value will be None.

Example myapp/views.py:

from django.views.generic.edit import CreateViewfrom myapp.models import Authorclass AuthorCreate(CreateView):    model = Author    fields = ['name']

Example myapp/author_form.html:

<form action="" method="post">{% csrf_token %}    {{ form.as_p }}    <input type="submit" value="Create" /></form>

UpdateView

class django.views.generic.edit.UpdateView

A view that displays a form for editing an existing object, redisplaying the form with validation errors (if there are any) and saving changes to the object. This uses a form automatically generated from the object’s model class (unless a form class is manually specified).

Ancestors (MRO)

This view inherits methods and attributes from the following views:

Attributes

template_name_suffix

The UpdateView page displayed to a GET request uses a template_name_suffix of '_form'. For example, changing this attribute to '_update_form' for a view updating objects for the example Author model would cause the default template_name to be 'myapp/author_update_form.html'.

object

When using UpdateView you have access to self.object, which is the object being updated.

Example myapp/views.py:

from django.views.generic.edit import UpdateViewfrom myapp.models import Authorclass AuthorUpdate(UpdateView):    model = Author    fields = ['name']    template_name_suffix = '_update_form'

Example myapp/author_update_form.html:

<form action="" method="post">{% csrf_token %}    {{ form.as_p }}    <input type="submit" value="Update" /></form>

DeleteView

class django.views.generic.edit.DeleteView

A view that displays a confirmation page and deletes an existing object. The given object will only be deleted if the request method is POST. If this view is fetched via GET, it will display a confirmation page that should contain a form that POSTs to the same URL.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

Attributes

template_name_suffix

The DeleteView page displayed to a GET request uses a template_name_suffix of '_confirm_delete'. For example, changing this attribute to '_check_delete' for a view deleting objects for the example Author model would cause the default template_name to be 'myapp/author_check_delete.html'.

Example myapp/views.py:

from django.views.generic.edit import DeleteViewfrom django.core.urlresolvers import reverse_lazyfrom myapp.models import Authorclass AuthorDelete(DeleteView):    model = Author    success_url = reverse_lazy('author-list')

Example myapp/author_confirm_delete.html:

<form action="" method="post">{% csrf_token %}    <p>Are you sure you want to delete "{{ object }}"?</p>    <input type="submit" value="Confirm" /></form>
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜宾县| 兴山县| 天柱县| 邹平县| 教育| 浑源县| 根河市| 辛集市| 涞水县| 青海省| 兰州市| 大埔县| 开封市| 洪洞县| 西青区| 肇州县| 富川| 内黄县| 九龙县| 晋州市| 屯门区| 阳高县| 甘孜县| 黄梅县| 海原县| 德钦县| 大埔县| 来安县| 松阳县| 铁岭市| 海晏县| 滁州市| 新津县| 武川县| 汉川市| 宜宾市| 沐川县| 项城市| 青岛市| 通榆县| 塔城市|