Web Programming -> Language
- python(django)
-
java(JEE, Servlet/JSP), spring mvc
-
node.js
-
golang
**가지고 있는 url을 사용자에게 보여주기 위한 목적
def get_absolute_url(self)
API : 프로그램을 사용하기 위한 약속
{{}} -> print()
--project main page 만들기
#mysite/urls.py
from django.contrib import admin
from django.urls import path, include
from mysite.views import HomeView
urlpatterns = [
path('admin/', admin.site.urls),
path('', HomeView.as_view(), name='home'),
path('bookmark/', include('bookmark.urls', namespace='bookmark')),
path('blog/', include('blog.urls', namespace='blog')),
]
**templateView
https://docs.djangoproject.com/en/3.1/ref/class-based-views/base
Base views | Django documentation | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
#mysite/views.py
from django.views.generic.base import TemplateView
class HomeView(TemplateView):
template_name = "home.html"
**STATICFILES_DIRS 참고
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
Settings | Django documentation | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
#mysite/settings.py
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
#templates/base.html
<!DOCTYPE html>
<html lang="ko">
<head>
<title>{% block title %}Django Web Programming{% endblock %}</title>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static 'css/base.css' %}{% endblock %}" />
<link rel="stylesheet" type="text/css" href="{% block extrastyle %}{% endblock %}" />
</head>
<body>
<div id="header">
<h2 class="maintitle">Easy&Fast Django Web Programming</h2>
<h4 class="welcome">Welcome, <a href="#">OOO</a> /
<a href="#">Change Password</a> /
<a href="#">Logout</a>
</h4>
</div>
<div id="menu">
<li><a href="{% url 'home' %}">Home</a></li> <!--/-->
<li><a href="{% url 'bookmark:index' %}">Bookmark</a></li> <!--/bookmark/-->
<li><a href="{% url 'blog:index' %}">Blog</a></li> <!--/blog/-->
<li><a href="#">Photo</a></li>
<li><a href="#">Add▽</a>
<ul>
<li><a href="#">Bookmark</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Photo</a></li>
</ul>
</li>
<li><a href="#">Change▽</a>
<ul>
<li><a href="#">Bookmark</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Photo</a></li>
</ul>
</li>
<li><a href="{% url 'blog:post_archive' %}">Archive</a></li>
<li><a href="#">Search</a></li>
<li><a href="{% url 'admin:index' %}">Admin</a></li>
</div>
{% block content %}{% endblock %}
{% block footer %}{% endblock %}
</body>
</html>
#templates/home.html
{% extends "base.html" %}
{% block title %}Welcome to Django Page!{% endblock %}
{% load static %}
{% block extrastyle %}{% static 'css/home.css' %}{% endblock %}
{% block content %}
<div id="content_home">
<div id="homeimg">
<img src="{% static 'img/django-actor-big.jpg' %}" style="height: 350px;" />
<h4 style="margin: 0;">This is Django posered web site.</h4>
</div>
</div>
{% endblock content %}
{% block footer %}
<div id="footer">
© This is FOOTER area.
</div>
{% endblock footer %}
#bookmark/templates/bookmark_list.html
{% extends "base.html" %}
{% block title %}Django Bookmark List{% endblock %}
{% block content %}
<div id="content">
<h1>Bookmark List</h1>
<ul>
{% for bookmark in object_list %}
<li><a href="{% url 'bookmark:detail' bookmark.id %}">{{ bookmark }}</a></li>
{% endfor %}
</ul>
</div>
{% endblock %}
#bookmark/templates/bookmark_detail.html
{% extends "base.html" %}
{% block title %}Django Bookmark Detail{% endblock %}
{% block content %}
<div id="content">
<h1>{{ object.title }}</h1>
<ul>
<li>URL: <a href="{{ object.url }}">{{ object.url }}</a></li>
</ul>
</div>
{% endblock %}
#blog/post_all.html(위 아래 이것만 추가하기 - 나머지 post.html에도 적용)
{% extends "base.html" %}
{% block title %}Post Page{% endblock %}
{% block content %}
<div id="content">
</div>
{% endblock %}






GET - 서버에 있는 정보를 저에게 주세요(read)
POST - 서버에 데이터를 요청 (create)
requestheader : 주소, 방식 requestbody : 서버에 전달하고 싶은 데이터
PUT - 수정(update) --- javascript 로 처리 해야 함
DELETE - 삭제(delete) --- javascript 로 처리 해야 함
*javascript 의 장점
=> ajax, jquery 등을 이용하여 put, delete 방식 전달가능
=> client 중심의 서비스 설계 가능
*golang
**융복합 프로젝트
- 빅데이터 : 수집, 전처리
- 클라우드 : 인프라 구축
- IoT : 디바이스 연결
- AI : 데이터 사용 분석
RESTful API: 리소스 상태
- http 프로토콜
-
get, post, put, delete만 사용가능
--Blog - 검색
- Django의 Q-객체 사용
- 테이블에 대한 쿼리를 처리
-
화면 UI 설계
-
Blog에 검색 기능 추가
-
Search 메뉴 사용
-
-
테이블 설계
-
변경 사항 없음
-
http://localhost:8000/?title="파이썬" (GET방식) -> url주소가 눈에 보임
*모델설계
/blog/search/ -> SearchFormView(FormView) -> post_search.html
#blog/urls.py
# /blog/search
path('search/', SearchFormView.as_view(), name='search'),
#blog/forms.py
# forms.py
from django import forms
class PostSearchForm(forms.Form):
search_word = forms.CharField(label = 'Search Word')
#blog/views.py
# Search 기능 추가
from blog.forms import PostSearchForm
from django.views.generic.edit import FormView
from django.db.models import Q
from django.shortcuts import render
class SearchFormView(FormView):
form_class = PostSearchForm # forms.py에 생성
template_name = "blog/post_search.html"
def form_valid(self, form):
# 입력값
schword = self.request.POST['search_word']
post_list = Post.objects.filter(Q(title__icontains=schword) | Q(description__icontains=schword) | Q(content__icontains=schword)).distinct()
# 검색결과도 함께 포함되어서 전달되어야 함
context = {}
context['form'] = form
context['search_keyword'] = schword
context['search_list'] = post_list
return render(self.request, self.template_name, context)
-> render함수로 같이 넣어서 페이지를 넘겨줘야 함.
#blog/templates/blog/post_search.html
{% extends "base.html" %}
{% block title %}검색 페이지{% endblock %}
{% block content %}
<div id="content">
<h1> Blog Search Engine </h1>
<form action="" method="POST">
<input type="text" name="search_word" value="{{ search_word }}">
<input type="checkbox">Bookmark
<input type="checkbox">Blog
<input type="submit" value="Search">
</form>
<br/><br/>
{% if search_list %}
{% for post in search_list %}
<h3>{{ post.title }}</h3>
{{ post.modify_date | date:"Y-m-d" }}
<p>{{ post.description }}</p>
{% endfor %}
{% elif search_keyword %}
[{{ search_keyword }}]의 검색 결과가 없습니다.
{% endif %}
</div>
{% endblock %}
{% block footer %}
<div id="footer">
© This is FOOTER area.
</div>
{% endblock %}
*form_valid() -> success_url
https://docs.djangoproject.com/en/3.1/topics/class-based-views/generic-editing
Form handling with class-based views | Django documentation | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
self -> 메소드를 호출하는 자기 자신
함수에는 모두 self를 인자로 가져야 한다.
*icontains
https://docs.djangoproject.com/en/3.1/ref/models/querysets
QuerySet API reference | Django documentation | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
'CLOUD > Django' 카테고리의 다른 글
1/25 Django - 복습(RESTful API) (0) | 2021.01.25 |
---|---|
1/21 Django - 복습 2차시(with VS Code) (0) | 2021.01.21 |
1/20 - Django 복습(with VS Code) (0) | 2021.01.20 |
1/15 Django 4차시 - blog app 만들기(Maria DB, 페이징처리, django REST frame) (0) | 2021.01.15 |
1/14 Django 3차시 - blog app만들기(CRUD + 로그인/로그아웃 + reply) (0) | 2021.01.14 |