我想尝试一下tutorial https://youtu.be/WNvxR8RFzBg并反映输入Home.html
,这是它们的总和result.html
使用在 Google Colab 笔记本中运行的 Django Server。
我可以在 Google Colab 介质中成功运行 Django 服务器,如下所示截屏 https://i.imgur.com/zEY1CbN.jpg你只需要manually set ALLOWED_HOSTS = ['colab.research.google.com']
in settings.py
一旦你安装完成django /questions/tagged/django您集群中的库。
问题是我无法沟通和反映结果Home.html
以及它们的总和result.html
.
I saw in the Tutorial the browser of the local machine when opening HTML docs are:
我尝试按照设置进行操作,但我仍然可以弄清楚如何在 Google Colab 介质中由 Django 运行的 HTML 文档之间进行通信。我想我已经接近解决方案了,但我仍然无法管理它。
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
请随时访问notebook https://colab.research.google.com/drive/12zdPUgaMxj-ilv6n_VWT9YmQiXVysBBC?usp=sharing以便快速排除故障。
您可以使用 Windows 快速创建 HTML 文档notepad /questions/tagged/notepad并保存它们.html
.
home.html
或从此下载link https://drive.google.com/file/d/1xFKNJpWsNrdHVfGHpGWrhVdN7Dt-Pw64/view?usp=sharing:
<h2> Welcome to my Web </h2>
<form action="result">
<input type="number" name="no1" placeholder="input1"/>
<input type="number" name="no2" placeholder="input2"/>
<input type="submit"/>
</form>
result.html
或从此下载link https://drive.google.com/file/d/12bC7heFKD_UDqRvwTbxmThpDi_Wrtwul/view?usp=sharing::
<h1>{{ answer }}</h1>
以下代码在 Google Colab 笔记本中用于反映以下输入Home.html
求和后为result.html
使用在 Google Colab 集群中运行的 Django Server。在运行 Pythonic 代码之前,HTML 文档也会更新并在集群中可用。
from django.shortcuts import render, HttpResponse
# Create your views here
def home(request):
return render(request, 'http://127.0.0.1:8000/home.html')
def result(request):
no1 = int(request.GET.get('no1') )
no2 = int(request.Get.get('no2') )
answer = no1 + no2
return render(request, 'http://127.0.0.1:8000/result.html', {'answer' : answer})
运行此单元格后:
!python /content/portfolio/manage.py runserver 8000
#Watching for file changes with StatReloader
#Performing system checks...
#System check identified no issues (0 silenced).
#You have 18 unapplied migration(s). Your project may not work properly #until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
#Run 'python manage.py migrate' to apply them.
#June 23, 2023 - 13:23:03
#Django version 4.2.2, using settings 'portfolio.settings'
#Starting development server at http://127.0.0.1:8000/
#Quit the server with CONTROL-C.
#[23/Jun/2023 13:23:03] "GET / HTTP/1.1" 200 10664
#Not Found: /favicon.ico
#[23/Jun/2023 13:23:04] "GET /favicon.ico HTTP/1.1" 404 2124
#[23/Jun/2023 13:28:07] "GET / HTTP/1.1" 200 10664
#Not Found: /favicon.ico
#[23/Jun/2023 13:28:08] "GET /favicon.ico HTTP/1.1" 404 2124
当我点击http://127.0.0.1:8000/ http://127.0.0.1:8000/给我
此站点无法访问 127.0.0.1 拒绝连接。
我仔细检查了代理设置,以确保我没有通过以下方式使用代理服务器:转到 Chrome 菜单 > 设置 > 显示高级设置... > 更改代理设置... > LAN 设置和deselect“为您的 LAN 使用代理服务器”。
如果我单击上一行生成的地址:
from google.colab.output import eval_js
print(eval_js("google.colab.kernel.proxyPort(8000)"))
#https://XXXXXXXX-XXXXXXXXXXXXX-8000-colab.googleusercontent.com/
I have :
我试图将求和输入 (10 + 20) 反映到result.html
不成功:http://127.0.0.1:8000/result?no1=10&no2=20
并再次得到:
此站点无法访问 127.0.0.1 拒绝连接。
如果有人能指导我如何在 [Google Colab] 中练习 Django,我将不胜感激(https://colab.research.google.com/ https://colab.research.google.com/)实践中的媒介?我找到这个post https://stackoverflow.com/q/60571301/10452700,但不确定是否是这样!
Edit1:今天,我尝试使用这个answer https://stackoverflow.com/a/46026551/10452700通过设置settings.py
不成功:
ALLOWED_HOSTS = ['*']
# start Django server using
#!python manage.py runserver 127.0.0.1:8000
!python manage.py runserver 0.0.0.0:8000
PS:我还在我的机器上激活了Localhost:截屏 https://i.imgur.com/O3IxG24.jpg
Edit2: I feel that probably instead of inserting 127.0.0.1:8000
in my browser, I need to insert https://XXXXXXXX-XXXXXXXXXXXXX-8000-colab.googleusercontent.com/
I think I also need to include some URL mapping https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Home_page#url_mapping in urls.py using TemplateView https://docs.djangoproject.com/en/2.0/topics/class-based-views/ based on this answer https://stackoverflow.com/a/50047075/10452700:
from django.contrib import admin
from django.views.generic import
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', TemplateView.as_view(template_name="home.html"))
但到目前为止,我仍然无法修复和建立 HTML 页面
并得到301 HTTP 错误在 Django 服务器状态中