from django.urls import path
from .views import (
PostListView,
PostDetailView,
PostCreateView,
PostUpdateView,
PostDeleteView,
UserPostListView,
UserProfileListView,
UserProfilePostListView
)
from . import views
urlpatterns = [
path('', PostListView.as_view(), name='blog-home'),
path('user/<str:username>/', UserProfileListView.as_view(), name='user-profile' ),
path('user/<str:username>/posts/', UserPostListView.as_view(), name='user-posts'),
path('user/<str:username>/posts/view/', UserProfilePostListView.as_view(), name='user-posts-view'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/<int:pk>/comment/', views.add_comment, name='add-comment'),
path('post/new', PostCreateView.as_view(), name='post-create'),
path('post/<int:pk>/edit/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
path('about/', views.about, name='blog-about')
]
from django.shortcuts import render, get_object_or_404from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixinfrom django.contrib.auth.models import Userfrom django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteViewfrom .models import Post
def home(request): context = { 'posts': Post.objects.all() } return render(request, 'blog/home.html', context)
def upsite(request): context = { 'uposts': UPost.objects.all() }
class PostListView(ListView): model = Post template_name = 'blog/home.html' context_object_name = 'posts' ordering =['-date_posted'] paginate_by = 5
class UserPostListView(ListView): model = Post template_name = 'blog/user_posts.html' context_object_name = 'posts' paginate_by = 5
def get_queryset(self): user = get_object_or_404(User, username=self.kwargs.get('username')) return Post.objects.filter(author=user).order_by('-date_posted')
class UserProfileListView(ListView): model = Post template_name = 'blog/user_profile.html' context_object_name = 'posts' paginate_by = 5
def get_queryset(self): user = get_object_or_404(User, username=self.kwargs.get('username')) return Post.objects.filter(author=user).order_by('-date_posted')
class UserProfilePostListView(ListView): model = Post template_name = 'blog/user_profile_posts.html' context_object_name = 'posts' paginate_by = 5
def get_queryset(self): user = get_object_or_404(User, username=self.kwargs.get('username')) return Post.objects.filter(author=user).order_by('-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 else: 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 else: return False
def about(request): return render(request, 'blog/about.html', {'title': 'About'})
def add_comment(request, slug): post = get_object_or_404(Post, slug=slug) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('blog:post_detail', slug=post.slug) else: form = CommentForm() template = 'blog/add-comment.html' context = { 'form': form } return render(request, template, context)
{% extends "blog/base.html" %}{% load crispy_forms_tags %}{% block content %} <div class="content-section"> <form method="POST"> {% csrf_token %} <fieldset class="form-group"> <legend class="border-bottom mb-4">Comment</legend> {{ form|crispy }} </fieldset> <div class="form-group"> <button class="btn btn-outline-warning" type="submit">Post Comment!</button> </div> </form> </div>{% endblock content %}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/aeaadfcb-38b7-4db2-9921-1c8c2485fd5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/aeaadfcb-38b7-4db2-9921-1c8c2485fd5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHfehoVJDE5aGBgfHk1pJJ_ffZoMwuOx3vengZNdmjxfsNSktg%40mail.gmail.com.
i fixed the error, thanks. But there is already a new one when i try to add a comment. D:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87fc38dd-6dac-4cb2-a6f5-81056728d65f%40googlegroups.com.
If i do that i get another error:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/769a8b8a-53d8-43fe-9caa-908cc6fc0204%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/769a8b8a-53d8-43fe-9caa-908cc6fc0204%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHfehoXQbi948m6-NOgeNvEUj4AWTZxcjjt0wShEO7-ajfgeFw%40mail.gmail.com.