반응형
CodingEntrepreneurs Django 강의 정리
models>
from django.conf import settings
from django.db import models
from django.db.models import Q
from django.utils import timezone
# Create your models here.
User = settings.AUTH_USER_MODEL
class BlogPostQuerySet(models.QuerySet):
def published(self):
now = timezone.now()
return self.filter(publish_date__lte=now)
def search(self, query):
lookup = (
Q(title__icontains=query)
| Q(content__icontains=query)
| Q(slug__icontains=query)
| Q(user__first_name__icontains=query)
| Q(user__last_name__icontains=query)
| Q(user__username__icontains=query)
)
return self.filter(lookup)
class BlogPostManager(models.Manager):
def get_queryset(self):
return BlogPostQuerySet(self.model, using=self._db)
def published(self):
return self.get_queryset().published()
def search(self, query=None):
if query is None:
return self.get_queryset().none()
return self.get_queryset().published().search(query)
class BlogPost(models.Model): # blogpost_set -> queryset
# id = models.IntegerField() # pk
user = models.ForeignKey(User, default=1, null=True, on_delete=models.SET_NULL)
image = models.ImageField(upload_to="image/", blank=True, null=True)
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True) # hello world -> hello-world
content = models.TextField(null=True, blank=True)
publish_date = models.DateTimeField(
auto_now=False, auto_now_add=False, null=True, blank=True
)
timestamp = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
objects = BlogPostManager()
class Meta:
ordering = ["-publish_date", "-updated", "-timestamp"]
def get_absolute_url(self):
return f"/blog/{self.slug}"
def get_edit_url(self):
return f"{self.get_absolute_url()}/edit"
def get_delete_url(self):
return f"{self.get_absolute_url()}/delete"
view.html>
{% extends "base.html" %}
{% block content %}
<div class='row'>
<div class='col-12 col-8 mx-auto'>
{% if query %}
<p class='lead'>You searched for {{ query }}</p>
{% else %}
<form class="form-inline my-2 my-lg-0" action='/search/'>
<input class="form-control mr-sm-2" type="search" name='q' placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
{% endif %}
{% for blog_item in blog_list %}
{% include 'blog/list-inline.html' with blog_post=blog_item truncate=True detail=False %}
{% endfor %}
</div>
</div>
{% endblock %}
views>
from django.shortcuts import render
from blog.models import BlogPost
from .models import SearchQuery
def search_view(request):
query = request.GET.get("q", None)
user = None
if request.user.is_authenticated:
user = request.user
context = {"query": query}
if query is not None:
SearchQuery.objects.create(user=user, query=query)
blog_list = BlogPost.objects.search(query=query)
context["blog_list"] = blog_list
return render(request, "searches/view.html", context)
반응형
'Python_WEB > Try_Django' 카테고리의 다른 글
[Django]Result (0) | 2020.06.27 |
---|---|
[Django]Putting it All Together (0) | 2020.06.27 |
[Django]Image Field and Uploading Images (0) | 2020.06.27 |
[Django]Static Files and Uploading Files (0) | 2020.06.21 |
[Django]Published and Draft Posts (0) | 2020.06.21 |