django templateview
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('about/', TemplateView.as_view(template_name="about.html")),
]
django templateview
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('about/', TemplateView.as_view(template_name="about.html")),
]
django form view
class ContactView(FormView):
form_class = ContactForm
template_name = 'contact-us.html'
success_url = reverse_lazy('<app_name>:contact-us')
def get_initial(self):
initial = super(ContactView, self).get_initial()
if self.request.user.is_authenticated:
initial.update({'name': self.request.user.get_full_name()})
return initial
def form_valid(self, form):
self.send_mail(form.cleaned_data)
return super(ContactView, self).form_valid(form)
def send_mail(self, valid_data):
# Send mail logic
print(valid_data)
django forms views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
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):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
form.send_email()
return super().form_valid(form)
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
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us