Best VPN Service (English Banners)

GeekBuying.com

Using Django 6 – Class Based Views

Alibaba Cloud
Autonomous Standing Desk

System Mechanic® Ultimate Defense™
Save up to 50% on ANSI Standards Packages

20191121-django

code(ALL)

CODE FOR THE VIEWS: blog/views.py

from django.shortcuts import render, get_object_or_404
from django.views.generic import (
CreateView,
DetailView,
ListView,
UpdateView,
DeleteView
)

from .forms import ArticleModelForm
from .models import Article

class ArticleCreateView(CreateView):
template_name = ‘articles/article_create.html’
form_class = ArticleModelForm
queryset = Article.objects.all() #<blog>/<modelname>_create.html
success_url=’/’ #GOBACKTOHOME METHOD1

def form_valid(self,form):
print(form.cleaned_data)
return super().form_valid(form)

# def get_success_url(self):
# return ‘/’ #GOBACKTOHOME METHOD2

class ArticleListView(ListView):
template_name = ‘articles/article_list.html’
queryset = Article.objects.all() #<blog>/<modelname>_list.html

class ArticleDetailView(DetailView):
template_name = ‘articles/article_detail.html’
# queryset = Article.objects.all() #when you use pk instead of id

def get_object(self):
id_=self.kwargs.get(“id”)
return get_object_or_404(Article, id=id_)
class ArticleUpdateView(UpdateView):
template_name = ‘articles/article_update.html’
form_class = ArticleModelForm
queryset = Article.objects.all() #<blog>/<modelname>_update.html

def form_valid(self,form):
print(form.cleaned_data)
return super().form_valid(form)

def get_object(self):
id_=self.kwargs.get(“id”)
return get_object_or_404(Article, id=id_)

Class Based Views – ListView

  1. models.py

from django.db import models

# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
active = models.BooleanField(default=True)

2. views.py

IK Multimedia's T-RackS 5

from django.shortcuts import render

# Create your views here.
from django.views.generic import (
CreateView,
DetailView,
ListView,
UpdateView,
DeleteView
)

from .models import Article

class ArticleListView(ListView):
queryset = Article.objects.all() #blog/modelname_list.html

3.urls.py –blog

from django.urls import path
from .views import (
ArticleListView
)

app_name =’articles’
urlpatterns = [
path(”, ArticleListView.as_view(), name=’article-list’),
# path(‘create/’, view_name, name=’article-create’),
# path(‘<int:id>/’, view_name, name=’article-detail’),
# path(‘<int:id>/update/’, view_name, name=’article-update’),
# path(‘<int:id>/delete’, view_name, name=’article-delete’),
]

4. urls.py –trydjango

urlpatterns = [
path(‘products/’, include(‘products.urls’)),
path(‘blog/’, include(‘blog.urls’)),
]

Screenshot 2019-11-22 at 10.09.57

Autonomous Standing Desk

5. ADD …

to resolve the problem

Class Based Views – DetailView

 

Class Based Views – CreateView (3:15:39)

  1. IMPORT MODEL FORM
  2. CREATE TEMPLATE HTML, PASTE FORM – RENDER OUT A FORM
  3. ADD path(‘create/’, ArticleCreateView.as_view(), name=’article-create’), TO URLPATTERNS
  4. GET BELOW “WITHOUT “FIELD” IS PROHIBITED”

Screenshot 2019-11-22 at 18.03.05

5. BRING IN FORM (ArticleModelForm) INTO THE CLASS

from .forms import ArticleModelForm
from .models import Article

class ArticleCreateView(CreateView):
template_name = ‘articles/article_create.html’
form_class = ArticleModelForm
queryset = Article.objects.all()

def form_valid(self,form):
print(form.cleaned_data)
return super().form_valid(form)

6. NOW THE CREATE PAGE WORKS, BUT AFTER ENTER DATA, IN THE TERMINAL IT SHOW CLEAN DATA (LATER, IF YOU GO TO blog/ YOU SEE IT’S THERE (ID=3)

Screenshot 2019-11-22 at 18.23.27

BUT IN THE BROWSER IT SAYS:

Either provide a url or define a get_absolute_url method on the Model.

Screenshot 2019-11-22 at 18.18.27

7. ADD get_absolute_url IN models.py AND IMPORT reverse

from django.db import models
from django.urls import reverse
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
active = models.BooleanField(default=True)

def get_absolute_url(self):
# return f”/products/{self.id}/”
return reverse(“articles:article-detail”, kwargs={“id”: self.id})

Class Based Views – UpdateView

PRETTY MUCH THE SAME STEPS AS ABOVE, SIMILAR TO DETAILVIEW

class ArticleUpdateView(UpdateView):
template_name = ‘articles/article_update.html’
form_class = ArticleModelForm
queryset = Article.objects.all() #<blog>/<modelname>_update.html

def form_valid(self,form):
print(form.cleaned_data)
return super().form_valid(form)

def get_object(self):
id_=self.kwargs.get(“id”)
return get_object_or_404(Article, id=id_)

Class Based Views – DeleteView

PRETTY MUCH THE SAME STEPS AS ABOVE, SIMILAR TO DETAILVIEW(?)

WE DONT HAVE A SUCESS URL YET SO WE ADD:

from django.urls import reverse

class ArticleDeleteView(DeleteView):
template_name = ‘articles/article_delete.html’
# queryset = Article.objects.all() #when you use pk instead of id
# success_url = ‘/blog/’ #CANNOT BE CALLED AS THE DATA HAS GONE

def get_object(self):
id_=self.kwargs.get(“id”)
return get_object_or_404(Article, id=id_)

def get_success_url(self):
return reverse(‘articles:article-list’)

 

 

nordvpn