这是我的代码form.py:
from django import forms
from .models import Category
class CategoryForm(forms.Form):
# class Meta:
# model = Category
# fields = ('title',)
title = forms.CharField(label = 'title', max_length=100)
这是我的view.py:
def CategoryForm(request):
if request.method == 'POST':
form = CategroyForm(request.POST)
if form.is_valid():
# category = form.save(commit = False)
# category.title = post
# category.save()
return redirect('posts/create_blog.html')
else:
form = CategoryForm()
template = 'posts/create_blog.html'
context = {'form':form}
return render(request,template,context)
这是我的view.py:
<form action= "CategoryForm" method = "POST" style = 'margin-top:0'>
{% csrf_token %}
<!--<label style='margin-left: 4%; color: gray;'>Choose Category Name</label>-->
{{ form.as_p }}
<input type = 'submit' value='Submit'>
</form>
我不太了解没有出现什么问题
我的model.py:
class Category(models.Model):
title = models.CharField(max_length=255)
def __str__(self):
self.title