기록하는 개발자

[Django] Wordcount 프로그램 본문

Web/Django

[Django] Wordcount 프로그램

밍맹030 2020. 5. 16. 15:09
728x90

<WORDCOUNT>

 

Home.html

1. about 페이지와 링크로 연결

2. 사용자들로부터 입력을 받음

3. 결과 제출 버튼

 

About.html

1.home 페이지와 링크로 연결

2,.about에 관련된 내용 적혀 있음

 

Result.html

1.home 에서 입력 받은 데이터를 처리한 함수를 받아 “결과는 이러하다”를 출력

 


View.py에 정의되어야 하는 함수

1. home을 띄우는 함수

2.about을 띄우는 함수

3.result에 전달할 함수(home에서 입력 받은 데이터를 처리하는 함수)à글자를 세주는 함수

 

만들 URL

1.home 을 띄우는 url=뒤에 아무것도 안 붙는 url(e.g. mutsawordcount.com)

2.about을 띄우는 url=/about(e.g. mutsawordcount.com/about)

3.result를 띄우는 url=/result(e.g. mutsawordcount.coun/result)

 


템플릿 언어       

-html안에 쓰는 장고 제공 언어

-html안에 파이썬 변수/문법을 쓰고 싶을 때 사용

템플릿 변수

-해당 파이썬 변수를 html파일에 담아 화면에 출력하라

 

템플릿 필터

{{value | length}} : value의 길이 반환

{{value | lower}} : value를 소문자로 출력

 

템플릿 태그

-html 상에서 파이썬 문법 사용, url 생성 등의 기능 제공

Ex) {%tag%}…태그내용…{%endtag%}}

>>html 태그처럼 장고의 템플릿 태그도 끝나는 태그가 있어야한다.

 

 e.g. for 문

파이썬 파일 HTML
Aclass=
["민수", "영희","철수"]
number of students={{class | length}}
{%for students in class%}
    {{students}}
{%endfor%}

 1. number of students={{class | length}}

class라는 템플릿 변수에 length라는 템플릿 필터를 씌워 class라는 변수의 길이를 반환하라

-number of students는 3 반환

 

2.{%for students in class%}

python code상의 for문을 html상으로 나타낸 것

-class 전체를 순회 하면서 한 번 씩 students에 변수를 담아라

 

3.{{students}}

템플릿 변수 부분에서는 students 변수를 출력해라.

 

4.{%endfor%}

-장고의 템플릿 태그를 닫아 for문 종료

 

 

e.g. URL생성

{%url ‘url_name’%}

 

<home.html>

<h1>안녕하세요 여기서 글자를 세보세요!</h1>
<a href="{%url 'about'%}">ABOUT</a>
<form action="{%url 'result'%}">
    <textarea cols="40" rows="10"name="fulltext"></textarea>
    <br>
    <input type="submit" value="count!">

</form>

<about.html>

<h1>About</h1>
<p>이곳은 글자를 세 주는 멋쟁이 사자처럼 wordcount 웹페이지 입니다.</p>
<a href="{%url 'home'%}">home으로 돌아가기</a>

<result.html>

<h1>당신이 입력한 텍스트는 {{total}} 로 구성되어 있습니다.</h1>
<a href="{% url 'home' %}">다시 하기</a>
<br>
<h2>입력한 텍스트 : </h2>
{{full}}

<h2>단어 카운트 : </h2>
{% for word, howmany in dictionary%}

{{word}} : {{howmany}}
<br>
{%endfor%}

<views.py>

from django.shortcuts import render

# Create your views here.
def home(request):
    return render(request,'home.html')
    
def about(request):
    return render(request, 'about.html')

def result(request):
    text=request.GET['fulltext']
    words=text.split()
    word_dictionary={}

    for word in words:
        if word in word_dictionary:
            #increase
            word_dictionary[word]+=1
        else:
            #add to dictionary
            word_dictionary[word]=1

    return render(request, 'result.html',{'full': text,'total':len(words),'dictionary': word_dictionary.items()})

   

<url.py>

from django.contrib import admin
from django.urls import path
import wordcount.views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',wordcount.views.home, name="home"),
    path('about/', wordcount.views.about, name="about"),
    path('result/', wordcount.views.result, name="result")
]

<첫 화면>

<count! 버튼 클릭 시 실행되는 화면>

<ABOUT 클릭 시 실행되는 화면>

728x90

'Web > Django' 카테고리의 다른 글

[Django] Template 상속  (0) 2021.05.27
[Django] 초기 환경설정 재정리  (0) 2021.05.11
[Django] REST API  (0) 2020.07.20
[Django] 내가 보려고 하는 정리  (0) 2020.07.20
[Django] 설치 및 가상 환경 실행  (0) 2020.05.16