首页 > 代码库 > 单选框 复选框
单选框 复选框
一.
#!/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‘)
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=http://www.mamicode.com/request.POST) if form.is_valid(): else: return render(request, ‘backend_add_article.html‘, {‘form‘: form}) else: return redirect(‘/‘)
<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=http://www.mamicode.com/"保 存"> </div> </form>
单选框 复选框
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。