单选框 复选框

一.

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from django.core.exceptions import ValidationError
from django import forms as django_forms
from django.forms import fields as django_fields
from django.forms import widgets as django_widgets

from repository import models


class ArticleForm(django_forms.Form):
    title = django_fields.CharField(
        widget=django_widgets.TextInput(attrs={'class': 'form-control', 'placeholder': '文章标题'})
    )
    summary = django_fields.CharField(
        widget=django_widgets.Textarea(attrs={'class': 'form-control', 'placeholder': '文章简介', 'rows': '3'})
    )
    content = django_fields.CharField(
        widget=django_widgets.Textarea(attrs={'class': 'kind-content'})
    )
    article_type_id = django_fields.IntegerField(
        widget=django_widgets.RadioSelect(choices=models.Article.type_choices)
    )
    category_id = django_fields.ChoiceField(
        choices=[],
        widget=django_widgets.RadioSelect
    )

    tags = django_fields.MultipleChoiceField(
        choices=[],
        widget=django_widgets.CheckboxSelectMultiple
    )

    def __init__(self, request, *args, **kwargs):
        super(ArticleForm, self).__init__(*args, **kwargs)
        blog_id = request.session['user_info']['blog__nid']
        self.fields['category_id'].choices = models.Category.objects.filter(blog_id=blog_id).values_list('nid',
                                                                                                         'title')
        self.fields['tags'].choices = models.Tag.objects.filter(blog_id=blog_id).values_list('nid', 'title')
forms.py
def add_article(request):
    
    if request.method == 'GET':
        form = ArticleForm(request=request)
        return render(request, 'backend_add_article.html', {'form': form})
    elif request.method == 'POST':
        form = ArticleForm(request=request, data=request.POST)
        if form.is_valid():
    else:
        return render(request, 'backend_add_article.html', {'form': form})
else:
    return redirect('/')
view.py
<form method="POST" action="/backend/add-article.html" novalidate>
            <div class="form-group">
                <label for="{{ form.title.id_for_label }}">标题 <span>{{ form.title.errors.0 }}</span></label>
                {{ form.title }}
            </div>
            <div class="form-group">
                <label for="summary">简介 <span>{{ form.summary.errors.0 }}</span></label>
                {{ form.summary }}
            </div>
            <div class="form-group">
                <label for="content">内容 <span>{{ form.content.errors.0 }}</span></label>
                {{ form.content }}
            </div>
            <div class="form-group">
                <label>类型 <span>{{ form.article_type_id.errors.0 }}</span></label>

                <div>
                    {{ form.article_type_id }}
                </div>

            </div>
            <div class="form-group">
                <label>分类 <span>{{ form.category_id.errors.0 }}</span></label>

                <div>
                    {{ form.category_id }}
                </div>
            </div>
            <div class="form-group">
                <label>标签 <span>{{ form.tags.errors.0 }}</span></label>

                <div>
                    {{ form.tags }}
                </div>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="保 存">
            </div>
        </form>
test.html

 

posted @ 2017-07-26 09:05  golangav  阅读(415)  评论(0编辑  收藏  举报