django generic create view code example
Example 1: django create view class
from .forms import CreateArticleForm
from django.views.generic import CreateView
class ArticleCreateView(CreateView):
form_class = CreateArticleForm
template_name = 'articles/create_article.html'
from .views import ArticleCreateView
urlpatterns =[ path('articles/create/', ArticleCreateView.as_view()),]
Example 2: formview django
from myapp.forms import ContactForm
from django.views.generic.edit import FormView
class ContactView(FormView):
template_name = 'contact.html'
form_class = ContactForm
success_url = '/thanks/'
def form_valid(self, form):
form.send_email()
return super().form_valid(form)
Example 3: django updateview class
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'
from .views import ArticleUpdateView
urlpatterns =[
path('articles/<int:pk>/update/', ArticleUpdateView.as_view()),]