WEB/Django prac

[Django prac][HTML 1] Fake naver 실습 1

bay07 2024. 3. 13. 16:12

 

내가 입력창에서 검색어를 입력하고, 엔터를 누르면

그 검색어가 네이버로 가서 검색이 될 수 있도록 만들기 


0. 코드 다운받고 셋팅

* 스켈레톤 코드 (비공개)

https://blog.naver.com/bayleaf07/223381919598

 

끈기와 인내를 통한 성장 ☆ : 네이버 블로그

Take pains with these things; be absorbed in them, so that your progress will be evident to all.

blog.naver.com

 


1. urls.py 입력

- 원래 상태 

 

- 바뀐 상태 

더보기
"""
URL configuration for firstpjt project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/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 articles import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index),
    path('dinner/', views.dinner),
    path('search/', views.search),
]

2. views.py 입력

- 원래 상태 

더보기
# 여기 주석 풀어주기
# foods 중에 랜덤으로 뽑아서 출력하려고
import random
from django.shortcuts import render


def index(request):
    context = {
        'name' : 'stella',
    }
    return render(request, 'articles/index.html', context)

def dinner(request):
    foods = [
        '사과',
        '딸기',
        '바나나',
        '감',
    ]
    picked = random.choice(foods)
    context = {
        'foods':foods,
        'picked':picked,
    }
    return render(request, 'articles/dinner.html', context)

- 바뀐 상태 

더보기
# 여기 주석 풀어주기
# foods 중에 랜덤으로 뽑아서 출력하려고
import random
from django.shortcuts import render


def index(request):
    context = {
        'name' : 'stella',
    }
    return render(request, 'articles/index.html', context)

def dinner(request):
    foods = [
        '사과',
        '딸기',
        '바나나',
        '감',
    ]
    picked = random.choice(foods)
    context = {
        'foods':foods,
        'picked':picked,
    }
    return render(request, 'articles/dinner.html', context)

def search(request):
    return render(request, 'articles/search.html')