Created
October 20, 2021 00:47
-
-
Save Marceloromeugoncalves/40445f043f1fe21d0d052d1942c17a94 to your computer and use it in GitHub Desktop.
Exemplo de CRUD com FBV no Django.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#urls.py | |
from django.urls import path | |
from .views import PostListView | |
from .views import PostDetailView | |
from .views import PostCreateView | |
from .views import PostUpdateView | |
from .views import PostDeleteView | |
from . import views | |
urlpatterns = [ | |
path('', PostListView.as_view(), name='blog-name'), | |
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'), | |
path('post/new/', PostCreateView.as_view(), name='post-create'), | |
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'), | |
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'), | |
path('about/', views.about, name='blog-about'), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from django.contrib.auth.mixins import LoginRequiredMixin | |
from django.contrib.auth.mixins import UserPassesTestMixin | |
from django.views.generic import ListView | |
from django.views.generic import DetailView | |
from django.views.generic import CreateView | |
from django.views.generic import UpdateView | |
from django.views.generic import DeleteView | |
from .models import Post | |
def home(request): | |
context = { | |
'posts': Post.objects.all(), | |
} | |
return render(request, 'blog/home.html', context) | |
class PostListView(ListView): | |
model = Post | |
template_name = 'blog/home.html' | |
context_object_name = 'posts' | |
ordering = ['-date_posted'] | |
class PostDetailView(DetailView): | |
model = Post | |
class PostCreateView(LoginRequiredMixin, CreateView): | |
model = Post | |
fields = ['title', 'content'] | |
def form_valid(self, form): | |
form.instance.author = self.request.user | |
return super().form_valid(form) | |
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView): | |
model = Post | |
fields = ['title', 'content'] | |
def form_valid(self, form): | |
form.instance.author = self.request.user | |
return super().form_valid(form) | |
def test_func(self): | |
post = self.get_object() | |
if self.request.user == post.author: | |
return True | |
return False | |
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView): | |
model = Post | |
success_url = '/' | |
def test_func(self): | |
post = self.get_object() | |
if self.request.user == post.author: | |
return True | |
return False | |
def about(request): | |
return render(request, 'blog/about.html', {'title': 'About'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment