update view django code example

Example 1: createview django

# Required imports
from django.urls import reverse_lazy

class YourView(CreateView):
	model 			= Model
    fields 			= ['your_fields']
    template_name 	= 'your_template'
    success_url		= reverse_lazy('home')
    
    def form_valid(self, form):
        form.instance.user = self.request.user
        super(YourView, self).form_valid(form)
        return redirect('home')

Example 2: django updateview

class YourView(generic.UpdateView):
	model			= YourModel
    fields			= ['your_fields']
    template_name	= 'your_template.html'
    success_url		= reverselazy('home')

Example 3: django edit model data in django view

emp = Employee.objects.get(pk = emp_id)
emp.name = 'Somename'
emp.save()

Example 4: django updateview class

###### views.py #####
from .models import Article
from .forms import  UpdateArticleForm
from django.views.generic import UpdateView
class ArticleUpdateView(UpdateView):
    model = Article
    form_class = UpdateArticleForm
    template_name = 'articles/create_article.html'
    
###### urls.py  ######
from .views import ArticleUpdateView
urlpatterns =[ 
  path('articles/<int:pk>/update/', ArticleUpdateView.as_view()),]
# pk is default value (for primary key of id of an instance of object