파이썬 Django 질문이욧!!!
조회수 310회
# index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="{%url 'index'%}" method="POST">
{% csrf_token %}
<label for="URL1">URL :</label>
<input type="text" name="URL" value="asdf"><br>
<input type="submit" value="전송">
<label for="">{{name_prop}} :</label>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
</form>
</body>
</html>
# views.py
from django.shortcuts import render
import requests
from bs4 import BeautifulSoup
from requests.exceptions import MissingSchema
# Create your views here.
def index(request):
global http_post_request
url = request.POST.get('URL')
data = {'urname' : "1", 'update':'update'} #update 넣어줘야 되네;
cookies={'login':'test%2Ftest'}
http_post_request = requests.post(url, data=data, cookies=cookies)
print(http_post_request.text)
# 오류 메시지
MissingSchema at /
Invalid URL 'None': No schema supplied. Perhaps you meant http://None?
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 4.1.5
Exception Type: MissingSchema
Exception Value:
Invalid URL 'None': No schema supplied. Perhaps you meant http://None?
Exception Location: C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py, line 390, in prepare_url
Raised during: main.views.index
Python Executable: C:\ProgramData\Anaconda3\python.exe
Python Version: 3.9.7
index.html에서 URL값을 입력해서 views.py로 이동하여 url 값을 넣어주고 http_post_request.txt 결과값을 확인하고 싶습니다.
그런데 url 값을 index.html에서 입력을 해주어야 view.py에서 실행이 될텐데
view.py가 그냥 바로 실행이 되어서 url값에 None이 뜨는거 같습니다.
이런 경우 어떻게 해야할까요?
1 답변
-
제가 django 는 해보지 못해서 자세한 방법은 아래를 참고해주시면 좋을것 같아요.
https://docs.djangoproject.com/ko/4.1/intro/tutorial01/#creating-a-project
하지만 보통 웹 프로젝트 구조에서의 백엔드, 프론트의 경우 백엔드에서 미리 제공하는 API 들에 대해 URL 을 선언해두고, 프론트가 호출하는 방식인데요.
그렇기 때문에 호출하려는 view 의 url 을 미리 정하신 후, 프론트에서 호출하시면 될 것 같습니다.
댓글 입력