CreateView example

models.py

1
2
3
4
5
class posts(models.Model):
    title = models.CharField(max_length=300, unique=True)
    time = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.title        

views.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from django.views.generic.edit import CreateView

#model
from .models import posts

class create_article(CreateView):
    model = posts #model
    fields = ['title'] # fields / if you want to select all fields use "__all__"
    template_name = 'CreateView_form.html' # templete for updating
    success_url="/posts-list" # posts list url

urls.py

1
2
#CreateView
    path('add-article/', create_article.as_view(), name="create_article"),

CreateView_form.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<html>
    <head>
        <title>CreateView_form</title>
        
    </head>

    <body>
<form method="POST">{% csrf_token %}
    
<!-- form variable -->    
 {{ form.as_p }} 

  <input type="submit" name="sumbit">
</form>
    </body>

</html>

English today is not an art to be mastered it's just a tool to use to get a result