这是我的表单类: from django import forms
from .models import Category
class AddResource(forms.Form):
CATEGORY_CHOICES = [cat.title for cat in Category.objects.all()]
ResourceTitle = forms.CharField()
Description = forms.Textarea()
Link = forms.URLField()
Categories = forms.MultipleChoiceField(choices=CATEGORY_CHOICES)
这是我对表单 URL 的看法:
from django.shortcuts import render
from django.views.generic import View, TemplateView, FormView
from .models import *
from .forms import AddResource
class AddResourceView(FormView):
template_name = 'addRes.html'
form_class = AddResource
success_url = '/'
def get_form(self, form_class):
return form_class()
def form_valid(self, form):
print form
return super(AddResourceView, self).form_valid(form)
我遇到的麻烦是在模板中调用它。如何调用我在 django-templates 中分配的表单?