TechTogetWorld


 daviduino.co.kr

techtogetworld.com

 david201207.blog.me

cafe.naver.com/3dpservicedavid


투표관련 웹 "vote" Django WEB을 통해 장고의 기능을 확인해 가는 과정입니다.

글의 순서는 아래와 같습니다.


===========================================================================

1. Page Not Found 404 발생시키는 방법
2. Page Not Found 404 발생시 다른 화면으로 넘기는 방법
3. templates 상속으로 코드 분량 줄이기
4. 네비게이션바 코드
  - 각 페이지의 공통 내용을 네비게이션에 등록
5. 화일 사용하기 : 아이콘 화일 적용
6. Ddjango web 시현 : pythonanywhere
7. 소스
8 참고자료

===========================================================================



[ Page Not Found 404 발생시키는 방법]


1. 예외 처리로 object가 없는 경우 HttpResponseNotFound 처리하기

# C:\Code\mysite\elections\views.py


from django.http import HttpResponseNotFound #추가


# 기존 코드 유지


def candidates(request, name):

    try :

        candidate = Candidate.object.get(name = name)

    except:

        return HttpResponseNotFound("없는 페이지 입니다.")

    return HttpResponse(candidate.name)

2. 예외 처리로 url이 없는 경우와, object가 없는 경우를 함께 처리하기

# C:\Code\mysite\elections\views.py


from django.http import Http404 #추가


# 기존 코드 유지


def candidates(request, name):

    try :

        candidate = Candidate.object.get(name = name)

    except:

        raise Http404

    return HttpResponse(candidate.name)

4. 예외 처리없이 url이 없는 경우와, object가 없는 경우를 함께 처리하기

# C:\Code\mysite\elections\views.py


from django.shortcuts import get_object_or_404 #추가


# 기존 코드 유지


def candidates(request, name):

    candidate = get_object_or_404(Candidate, name = name)

    return HttpResponse(candidate.name)


[ Page Not Found 404 발생시 다른 화면으로 넘기는 방법]

1. 디버그 설정과 디렉토리 설정 바꿔주기

# C:\Code\mystie\settings.py


# ...


DEBUG = False #True에서 False로 변경합니다


ALLOWED_HOSTS = ['localhost']


# ...


TEMPLATES = [

    {

        # ...

        'DIRS' : ['templates'],

        # ...

    }

]

2. 404파일 만들어주기

<!--C:\Code\mysite\templates\404.html-->

없는 페이지 입니다.


[ templates 상속으로 코드 분량 줄이기]


1. layout.html 설정

<!-- C:\Code\mysite\elections\templates\elections\layout.html-->


<!DOCTYPE html>

<html lang="en">

<head>

  <title>{% block title %}{% endblock %}</title>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</head>

<body>

{% block content %}{% endblock %}

</body>

</html>

2. 각 html파일에 layout.html 상속받기

1. index.html

<!DOCTYPE html>부터 </head>까지 삭제

<body>, </body>, </html> 태그 제거

<!-- C:\Code\mysite\elections\templates\elections\index.html-->


{% extends "elections/layout.html" %}

{% block title %}

선거 후보

{% endblock %}


{% block content %}


<!-- 기존 코드 유지.

    <div class = "container">로 시작하고 </table>로 끝납니다.-->


{% endblock %}


2. area.html

<!DOCTYPE html>부터 </head>까지 삭제

<body>, </body>, </html> 태그 제거

<!-- C:\Code\mysite\elections\templates\elections\area.html-->

{% extends "elections/layout.html" %}


{% block title %}

{{area}}

{% endblock %}


{% block content %}


<!-- 기존 코드 유지.

    <div class = "container">로 시작하고 </div>로 끝납니다.-->


{% endblock %}

3. result.html

<!DOCTYPE html>부터 </head>까지 삭제

<body>, </body>, </html> 태그 제거

<!-- C:\Code\mysite\elections\templates\elections\result.html-->

{% extends "elections/layout.html" %}


{% block title %}

{{area}} 여론조사 결과

{% endblock %}


{% block content %}


<!-- 기존 코드 유지.

    <div class = "container">로 시작하고 </div>로 끝납니다.-->


{% endblock %}



[네비게이션바 코드]


1. layout 추가하기

<!-- C:\Code\mysite\elections\templates\elections\layout.html -->


<!--기존 코드 -->

<body>

<nav class="navbar navbar-default">

  <div class="container-fluid">

    <div class="navbar-header">

      <a class="navbar-brand" href="{% url 'elections:home' %}">사이트명</a>

    </div>

  </div>

</nav>

<!--기존 코드 -->

2. url설정

# C:\Code\mysite\elections\urls.py

# 기존 코드 유지

app_name = 'elections'

urlpatterns = [

    url(r'^$', views.index, name = 'home')

    # 기존 코드 유지


[화일 사용하기]


1. 아이콘 적용하기

   1) static 파일은 DEBUG = true  상태에서 사용가능:C:\Code\mysite\mysite\settings.py 에서 DEBUG =true

   2) C:\Code\mysite\elections\static\elections =>favicon 그림화일 저장

   3) 경로 추가하기

<!-- C:\Code\mysite\elections\layout.html -->

{% load staticfiles %} <!-- 추가 -->


<!--기존 유지 <head> 태그 아래 코드추가>

    <link rel ="shortcut icon" type = "image/x-icon" href="{% static 'elections/favicon.ico' %}" />


[소스코드]

20170916 2 vote.zip



[참고자료]