1# Required imports
2from django.urls import reverse_lazy
3
4class YourView(CreateView):
5 model = Model
6 fields = ['your_fields']
7 template_name = 'your_template'
8 success_url = reverse_lazy('home')
9
10 def form_valid(self, form):
11 form.instance.user = self.request.user
12 super(YourView, self).form_valid(form)
13 return redirect('home')
1###### views.py #####
2from .forms import CreateArticleForm
3from django.views.generic import CreateView
4class ArticleCreateView(CreateView):
5 form_class = CreateArticleForm
6 template_name = 'articles/create_article.html'
7
8###### urls.py ######
9from .views import ArticleCreateView
10urlpatterns =[ path('articles/create/', ArticleCreateView.as_view()),]
1###### views.py #####
2from .models import Article
3from .forms import UpdateArticleForm
4from django.views.generic import UpdateView
5class ArticleUpdateView(UpdateView):
6 model = Article
7 form_class = UpdateArticleForm
8 template_name = 'articles/create_article.html'
9
10###### urls.py ######
11from .views import ArticleUpdateView
12urlpatterns =[
13 path('articles/<int:pk>/update/', ArticleUpdateView.as_view()),]
14# pk is default value (for primary key of id of an instance of object