Bootstrap

Django create view

import

from django.views import generic

classbased view

class WriteBlog(LoginRequiredMixin, generic.CreateView):
    login_url = '/account/login/'
    model = Blog
    template_name = 'write_blog.html'
    fields = ['category', 'title', 'content']
    success_url = '/blogs/'

    def form_valid(self, form):
        form.instance.author = self.request.user
        form.save()
        return redirect(self.success_url)

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Write Blog</title>
</head>
<body>
<form action="{% url 'mblogs:write_blog'%}" method="post">
    {% csrf_token %}
    {{form.as_p}} <!--WriteBlog View 中定义的fields-->
    <button type="submit">Submit</button>
</form>
</body>
</html>
;