Python Django is a question!!!

Asked 1 years ago, Updated 1 years ago, 430 views

# 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 must be included;
    cookies={'login':'test%2Ftest'}
    http_post_request = requests.post(url, data=data, cookies=cookies)
    print(http_post_request.text)
# error message
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

I'd like to enter a URL value in index.html and go to views.py to enter the url value and check the http_post_request.txt result value.

However, you have to enter the url value in index.html to run it on view.py.

I think view.py just runs right away, so the url value shows None.

What should we do in this case?

python django

2023-01-24 01:08

1 Answers

I haven't tried django, so please refer to the below for detailed instructions.

https://docs.djangoproject.com/ko/4.1/intro/tutorial01/#creating-a-project

However, usually the backend in the web project structure, and in the case of the front, the URL is declared for the APIs provided in advance by the backend, and the front is called.

Therefore, you can choose the url of the view you want to call in advance and call it at the front desk.


2023-01-24 04:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.