django FormView

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A more detailed ListView and DetailView in the Django official document detailed examples and descriptions, will not repeat it here, the purpose of this paper is mainly focused on the official document does not specify FormMixin, see examples below, we use the website to provide models:

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    class Meta:
        ordering = ["-name"]

    def __unicode__(self):
        return self.name

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField('Author')
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

The model provides 2 classes, one is Publisher, one is Book. below to see how to use FormMixin to update Book:

First of all, we need to create a form, the code as follows:

from django import forms
from models import Book

class BookForm(forms.ModelForm):
    class Meta:
        model = Book

So in our view class simply to operate:

from django.views.generic.edit import CreateView
from models import Book
from forms import BookForm

class AddBook(CreateView):
    form_class = BookForm
    model = Book
    template_name = 'addbook.html' #Here is your template file name

Then we create the template file, such as:

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

The final step is in the URLs file pointing to our AddBook class to create, for example:

url(r'^createbook/$', AddBook.as_view(), name = 'createbook'),

Complete the above steps can create Book! Simple? As if there are points, how non logged in user can also create Book? This problem we can refer to the official document in the Django example, we can add a method to the AddBook class, as follows:

from django.views.generic.edit import CreateView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from models import Book
from forms import BookForm

class AddBook(CreateView):
    form_class = BookForm
    model = Book
    template_name = 'addbook.html' #Here is your template file name

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(ProtectedView, self).dispatch(*args, **kwargs)

So, when the user wants to create Book without land, the browser will jump to the login screen
Write so it feels a bit chaotic, tidy, in-depth applications continue to write FormMixin applications of tomorrow


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Benedict at February 22, 2014 - 4:01 AM