본문 바로가기

전체 글192

14. Django 모듈을 활용한 태그 달기 pip install django-taggit pip install django-taggit-templatetags2 Pycharm의 경우 interpreter로 깔아도 된다. 중요! Django한테 다운받았음을 알려줘야한다. Project/settings.py INSTALLED_APPS = [ '''''' 'taggit.apps.TaggitAppConfig', 'taggit_templatetags2', ] TAGGIT_CASE_INSENSITIVE = True TAGGIT_LIMIT = 50 다음은 적용단계. tweet/models.py에 추가를 해준다. from taggit.managers import TaggableManager # Create your models here. class Tweet(.. 2022. 5. 30.
13. 서비스 수정 1. 로그인/회원가입 - 로그인을 했을시 왜 로그인이 잘 안되는지 알려주기 1) 회원가입 #user/views.py def sign_up_view(request): '''''' elif request.method == 'POST': #None->''로 변경 username = request.POST.get('username', '') password = request.POST.get('password', '') password2 = request.POST.get('password2', '') bio = request.POST.get('bio', '') if password != password2: #패스워드가 같지 않다고 알람 return render(request, 'user/signup.html', .. 2022. 5. 30.
12. Many-To-Many 모델 생성(related_name) from django.db import models from django.contrib.auth.models import AbstractUser from django.conf import settings #projects/settings class UserModel(AbstractUser): class Meta: db_table = "my_user" bio = models.CharField(max_length=256, default='') follow = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name='followee') 다시 makemigrations, migrate를 해주고 원할한 비교를 위해 6명의 회원을 추가로 가입해준다. 이후, ad.. 2022. 5. 30.
11. Django Shell Django Shell은 Django의 기능들을 코딩 없이 사용할 수 있게 해주는 기능이다. 주로 데이터를 확인하고 테스트 해보는 용도로 사용된다. python manage.py shell >>> #이게 나오면 정상 실행된것이다. >>> from restaurant.models import MyTopping, MyPizza >>> MyPizza.objects.all() >>> MyPizza.objects.get(pizza_name='Prperoni Pizza') >>> MyPizza.objects.get(pizza_name='Cheese Pizza') >>> MyPizza.objects.get(pizza_name='Cheese Pizza').pizza_topping.all() 당연하겠지만 .pizza_t.. 2022. 5. 30.
10. Django 데이터베이스 관계 1. one-to-many 하나가 여러개가 대응이 되는것이다. 하단의 코드를 보자. Tweet Model의 author는 ForeignKey로써 하나의 유저만이 들어갈 수 있다. 즉, 한 개의 글에는 한 명의 작성자만이 글을 쓸 수 있다. 반대로 한 명의 사용자가 여러개의 글을 작성할 수 있다. 이러한 관계가 one-to-many이다. class UserModel(AbstractUser): class Meta: db_table = "my_user" bio = models.TextField(max_length=500, blank=True) class TweetModel(models.Model): class Meta: db_table = "tweet" author = models.ForeignKey(User.. 2022. 5. 30.
9. Django 게시글 읽기/삭제 1. 읽기 읽기는 그냥 GET인데??? 맞다. 그냥 GET이다. 그리고 데이터를 받아오는것도 GET이다. 생각해보니 전에 7강에서 작성한 내용을 database에서 받아오는 행위가 없다. 이를 구현하자. 그렇다 tweet/views.py GET을 수정하면 된다. def tweet(request): if request.method == 'GET': user = request.user.is_authenticated if user: #수정된 부분 all_tweet = Tweet.objects.all().order_by('created_at') #만든 순으로 정렬 return render(request, 'tweet/home.html', {'tweet':all_tweet}) else: return redirec.. 2022. 5. 27.
8. Django 게시글 쓰기 게시글을 작성하기 위해 요 부분을 수정할 것이다. tweet/home.html 수정 전 나의 이야기를 적어주세요 작성하기 수정 후 나의 이야기를 적어주세요 {% csrf_token %} 작성하기 tweet/urls.py에는 이미 작성이 되어있기에 views.py로 넘어가준다. tweet/views.py from .models import Tweet def tweet(request): elif request.method == 'POST': user = request.user my_tweet = Tweet() my_tweet.author = user my_tweet.content = request.POST.get('my-content', '') my_tweet.save() return redirect('/t.. 2022. 5. 27.
7. Django 로그인 이후 기능 전까지는 로그인, 회원가입, admin(관리자) 페이지를 만들었다면 이제는 로그인 후의 페이지를 만들어야 된다. 쉽게 말해서 '/'경로의 페이지를 만들어주는 과정을 진행할 것이다. 1. templates/tweet/home.html {% extends 'base.html' %} {% block content %} Card title Card subtitle Some quick example text to build on the card title and make up the bulk of the card's content. 나의 이야기를 적어주세요 작성하기 Media heading Will you do the same for me? It's time to face the music I'm no longe.. 2022. 5. 27.
6. Django에서 제공하는 사용자 기능 처음부터 createsuperuser로 admin 페이지에 접근할 수 있는것은 Django 자체에서 'User모델'과 '로그인/로그아웃'을 제공하기 때문이다. 1. auth_user(기본 제공) vs my_user(커스텀) 기본 제공 모델이랑 커스텀 모델이랑 차이가 나는것이 보인다. 그래서 기본 제공 모델을 상속 받아서 커스텀 모델을 만들고자 한다. 2. 커스텀 User모델 수정하기 #user/models.py from django.db import models from django.contrib.auth.models import AbstractUser #Django의 유저 생성 모델을 임포트한다 class UserModel(AbstractUser): #임포트 받은 기본 유저 모델을 상속하고 class .. 2022. 5. 27.