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


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Write Blogtitle>
head>
<body>
<form action="{% url 'mblogs:write_blog'%}" method="post">
    {% csrf_token %}
    {{form.as_p}} 
    <button type="submit">Submitbutton>
form>
body>
html>

你可能感兴趣的:(Django)