-
Web Dev Bootcamp TIL Day-28(Django Start)TIL 2022. 5. 26. 22:20
MVT model flow Django's client-server interaction operates on a MVT(Model/View/Template) pattern
- The model is a layout of the data we will use in a specific app
- ORM(Object Relational Mapping): a python class is used as a data model and migrated to our local database
- view can be considered as the functionality of the application
- ex) returning a HttpResponse, rendering a template ...
- a template is what is shown to the client
creating a custom usermodel
- django comes w/ a prepackaged user model, but we can create our own usermodel
- notice that we can name our db_table by declaring its name in the meta class
- we can add this usermodel to our adminpage via admin.py
class UserModel(models.Model): class Meta: db_table = "my_user" username = models.CharField(max_length=20, null=False) password = models.CharField(max_length=256, null=False) bio = models.CharField(max_length=256, default='') created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True)
create a relational database
- notice that the author of a tweet uses the usermodel we created earlier as a foreignkey
from django.db import models from user.models import UserModel # Create your models here. class TweetModel(models.Model): class Meta: db_table = "tweet" author = models.ForeignKey(UserModel, on_delete=models.CASCADE) content = models.CharField(max_length=256) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True)
creating dynamic templates
- we can use jinja syntax to extend a base template and include different content
<title>{% block title %}{% endblock %} | spartaSNS</title> <body> {% block content %} {% endblock %} </body>
login/signup exercise
integrating GET/POST methods to html and views for login/signup
- note that django has a built-in security feature csrf_token
- without using JS, we can declare a method and action(route) within a <form> tag to send request data
- <input> tags and their corresponding names can match datafields
<form class="form-area" method="post" action="/sign-up/">
def sign_up_view(request): if request.method == 'GET': return render(request, 'user/signup.html') elif request.method == 'POST': username = request.POST.get('username', None) password = request.POST.get('password', None) password2 = request.POST.get('password2', None) bio = request.POST.get('bio', None) #check if username is already is in use by querying from database try: user_info = UserModel.objects.get(username=username) #if so, redirect to signup.html if user_info: return redirect('/sign-up') #if username is not in use except UserModel.DoesNotExist: #check that the password is written correctly if password == password2: # if so, create a new user and save it to DB new_user = UserModel() new_user.username = username new_user.password = password new_user.bio = bio new_user.save() else: return redirect('/sign-up') return redirect('/sign-in')
'TIL' 카테고리의 다른 글
Web Dev Bootcamp TIL Day-31 (Django CRUD) (0) 2022.05.30 Web Dev Bootcamp TIL Day-30(Django Cont: user authorization) (0) 2022.05.27 Web Dev Bootcamp TIL Day-27(Machine Learning Proj. KPT) (0) 2022.05.25 Web Dev Bootcamp TIL Day-22(Machine Learning Proj. Start) (0) 2022.05.20 Web Dev Bootcamp TIL Day-21(Deep Learning) (0) 2022.05.17 - The model is a layout of the data we will use in a specific app