๐Ÿ“’ [ TIL ] 2022.05.31_31์ผ์ฐจ # Django ๊ฐ•์˜ํ•™์Šต(4)

๋ฌธ๋ช…์ฃผยท2022๋…„ 5์›” 31์ผ
0
post-thumbnail

[ 2022-05-31 (ํ™”) ์˜ค๋Š˜์˜ TIL ]

[ Today Learn ]

  • ์žฅ๊ณ  ํƒœ๊ทธ ์„œ๋น„์Šค ์ ์šฉํ•˜๊ธฐ

โœ๏ธ ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒƒ, ์–ป์€๊ฒƒ

* ์žฅ๊ณ ํƒœ๊ทธ๋ชจ๋“ˆ ํ™œ์šฉํ•˜๊ธฐ

(1) ๊ฐ€์žฅ๋จผ์ € ์•„๋ž˜์™€ ๊ฐ™์ด ํƒœ๊ทธ๋ชจ๋“ˆ์„ ๋‹ค์šด๋ฐ›์•„์ค€๋‹ค.

pip install django-taggit
pip install django-taggit-templatetags2

(2) settings.py์— INSTALLED_APPS ๋ถ€๋ถ„์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ ํ•ด์ค€๋‹ค.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tweet',
    'user',
    'restaurant',
    'taggit.apps.TaggitAppConfig',
    'taggit_templatetags2',
]
#ํƒœ๊ทธ๋ชจ๋“ˆ์— ํ•„์š”ํ•œ ์ •๋ณด
TAGGIT_CASE_INSENSITIVE = True
TAGGIT_LIMIT = 50

(3) Tag๋Š” tweet๋ชจ๋ธ ์•ˆ์— ๋„ฃ์„ ๊ฒƒ์ด๋ฏ€๋กœ ํ•ด๋‹น models.py์—์„œ ์ž„ํฌํŠธ๋ฅผ ํ•ด์ฃผ๊ณ  ๋ชจ๋ธ์•ˆ์— tags๋ฅผ ์ •์˜ํ•ด์ค€๋‹ค.

# tweet/models.py
from taggit.managers import TaggableManager

class TweetModel(models.Model):
    class Meta:
        db_table = "tweet"

    author = models.ForeignKey(UserModel, on_delete=models.CASCADE)
    content = models.CharField(max_length=256)
    tags = TaggableManager(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

(4) ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜์˜€์œผ๋‹ˆ db์— makemigrations์™€ migrate๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์•Œ๋ ค์ฃผ๊ณ  ์ ์šฉ์„ ์‹œํ‚จํ›„ ์•„๋ž˜์™€ ๊ฐ™์ด ํ™”๋ฉด์—์„œ 'ํƒœ๊ทธ'๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

# tweet/views.py ์˜ tweet ํ•จ์ˆ˜

@login_required
def tweet(request):
    if request.method == 'GET':  # ์š”์ฒญํ•˜๋Š” ๋ฐฉ์‹์ด GET ๋ฐฉ์‹์ธ์ง€ ํ™•์ธํ•˜๊ธฐ
        all_tweet = TweetModel.objects.all().order_by('-created_at')
        return render(request, 'tweet/home.html', {'tweet': all_tweet})
    elif request.method == 'POST':  # ์š”์ฒญ ๋ฐฉ์‹์ด POST ์ผ๋•Œ
        user = request.user  # ํ˜„์žฌ ๋กœ๊ทธ์ธ ํ•œ ์‚ฌ์šฉ์ž๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
        content = request.POST.get('my-content', '')
        tags = request.POST.get('tag', '').split(',')
        if content == '':
            all_tweet = TweetModel.objects.all().order_by('-created_at')
            return render(request, 'tweet/home.html', {'error': '๊ธ€์€ ๊ณต๋ฐฑ์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค', 'tweet': all_tweet})
        else:
            my_tweet = TweetModel.objects.create(author=user, content=content)
            for tag in tags:
                tag = tag.strip()
                if tag != '': # ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์— ์ €์žฅํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ
                    my_tweet.tags.add(tag)
            my_tweet.save()
            return redirect('/tweet')

django-taggit ๋ชจ๋“ˆ์—์„œ '์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”' ๋ผ๊ณ  ์ •ํ•ด๋†“์€ ํด๋ž˜์Šค๋“ค์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

class TagCloudTV(TemplateView):
    template_name = 'taggit/tag_cloud_view.html'


class TaggedObjectLV(ListView):
    template_name = 'taggit/tag_with_post.html'
    model = TweetModel

    def get_queryset(self):
        return TweetModel.objects.filter(tags__name=self.kwargs.get('tag'))

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['tagname'] = self.kwargs['tag']
        return context
  • TagCloudTV ํด๋ž˜์Šค : ํƒœ๊ทธ๋“ค์„ ๋ชจ์•„๋†“๋Š” ํƒœ๊ทธํด๋ผ์šฐ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ์—ญํ• 

  • TaggedObjectLV ํด๋ž˜์Šค : ํƒœ๊ทธ๋“ค์„ ๋ชจ์•„์„œ ํ™”๋ฉด์— ์ „๋‹ฌํ•˜๋Š” ์—ญํ• 

(5) ๋งˆ์ง€๋ง‰์œผ๋กœ model๊ณผ view๊ฐ€ ์„ค์ •์ด ๋˜์—ˆ์œผ๋‹ˆ url์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

urlpatterns = [
    path('', views.home, name='home'),
    path('tweet/', views.tweet, name='tweet'),
    path('tweet/delete/<int:id>', views.delete_tweet, name='delete-tweet'),
    path('tweet/<int:id>', views.detail_tweet, name='detail-tweet'),
    path('tweet/comment/<int:id>', views.write_comment, name='write-comment'),
    path('tweet/comment/delete/<int:id>', views.delete_comment, name='delete-comment'),
    **path('tag/', views.TagCloudTV.as_view(), name='tag_cloud'),**
    **path('tag/<str:tag>/', views.TaggedObjectLV.as_view(), name='tagged_object_list'),**
]

๐ŸŒฑ ๋Š๋‚€ ์ 

ํ™•์‹คํžˆ flask๋ณด๋‹ค๋Š” django์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ๋“ค์ด ๋งŽ๊ธฐ๋•Œ๋ฌธ์— ์นœ์ ˆํ•œ ๋Š๋‚Œ์ด ๊ฐ•ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฅผ ๋‚ด๊ฐ€์›ํ•˜๋Š”๋Œ€๋กœ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ๊ฒƒ์—๋Š” ๋” ๋งŽ์€ ๋…ธ๋ ฅ์ด ํ•„์š”ํ•  ๊ฒƒ๊ฐ™๋‹ค. ๊ณง ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋˜ ์•ž๋‘๊ณ ์žˆ๊ธฐ๋•Œ๋ฌธ์— django๋ฅผ ํ™œ์šฉํ•ด์„œ ์ž˜ํ•  ์ˆ˜ ์žˆ์„์ง€ ๋ถ€๋‹ด์ด ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ“ ๋ฐฐ์šด ์‹ค๋ ฅ์ด๊ธฐ๋„ํ•˜๊ณ , ๊ฒฐ๊ณผ๋ฌผ๋ณด๋‹ค๋Š” ์—ด์‹ฌํžˆ ํ•œ ๊ณผ์ •์„ ๋‚จ๊ธฐ๊ณ  ์‹ถ๋‹ค.

๐ŸŽ ์ž์„ธํ•œ ์ฝ”๋“œ๋Š” Moonmoo ์— ๊ฒŒ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค

profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 5์›” 31์ผ

์˜ค๋Š˜๋„ ์˜ˆ์™ธ์—†๋Š” TIL!! ๋Œ€๋‹จํ•˜์‹ญ๋‹ˆ๋‹ค :)

1๊ฐœ์˜ ๋‹ต๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด