본문 바로가기

Django

django1 - static 폴더 (image, css, js)

 

게시글 참고

2022.10.14 - [개발을 위한 준비/설치 및 설정] - 장고 ip 주소 및 포트 번호 기본 값 설정

 

이전게시글 참고

2022.10.14 - [Django] - Django 시작하기 - html 출력 및 forward

 

urls

"""django1 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
    path("admin/", admin.site.urls),
    path('', views.indexFunc),
    path('hello', views.helloFunc),
]

 

views.py

from django.shortcuts import render
from django.http.response import HttpResponse

# Create your views here.
def indexFunc(request):
    """
    msg = "장고 만세"
    ss = "<html><body>장고 프로젝트 처리 %s</body></html>"%msg
    # return HttpResponse('요청 처리')
    return HttpResponse(ss)
    """
    
    # 클라이언트에게 html 파일을 반환 - 파이썬 값을 html에 담아서 전달
    msg = "장고 만세"
    context = {'msg':msg}   #dict type으로 작성해 html 문서에 기술한 장고 template 기호와 매핑
    return render(request, 'main.html', context)    # forward 방식 기본
    
def helloFunc(request):
    return render(request, 'show.html')

 

show,html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="/static/css/test.css">

<<!--  
<script type="text/javascript">
window.onload = function(){
	alert("자바스크립트 성공");
}
</script>
-->

<script type="text/javascript" src="/static/js/test.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	//alert("자바스크립트 jquery 성공");
	$("#myImg").click(function(){
		abcFunc();
	});
});
</script>

</head>
<body>
<h2>hello 요청에 대한 반응 문서</h2>
<img src="/static/images/logo.png" id="myImg">

</body>
</html>

 

'Django' 카테고리의 다른 글

django5 - DB  (0) 2022.10.17
django4 - session 예제(간이 쇼핑몰, 장바구니 기능)  (0) 2022.10.17
django3 - session  (0) 2022.10.17
django2 - GET, POST, django 기본 구조  (0) 2022.10.17
Django 시작하기 - html 출력 및 forward  (0) 2022.10.14