python - 无法从 DJANGO 获取输入/输出
问题描述
我是 Django 的新手。在练习时。我下载了 HTML 模板并设法修改它以加载 django。我的问题是我无法从 Django<>HTML 获得任何输入或输出。我不知道问题出在哪里。
视图.py
from django.shortcuts import render
def home(request):
return render(request, 'home.html' ,{})
def contact(request):
if request.method == "POST" :
username_1 = request.POST["username"]
return render(request, 'home.html', {'username_1' : username_1})
login_page/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
]
主页.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login V1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
<link rel="icon" type="image/png" href="{%static 'login_page/images/icons/favicon.ico' %}"/>
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/vendor/bootstrap/css/bootstrap.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/fonts/font-awesome-4.7.0/css/font-awesome.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/vendor/animate/animate.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/vendor/css-hamburgers/hamburgers.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/vendor/select2/select2.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/css/util.css' %}">
<link rel="stylesheet" type="text/css" href="{%static 'login_page/css/main.css' %}">
<!--===============================================================================================-->
</head>
<body>
<div class="limiter">
<div class="container-login100">
<div class="wrap-login100">
<div class="login100-pic js-tilt" data-tilt>
<img src={%static 'login_page/images/img-01.png'%} alt="IMG">
</div>
<form class="login100-form validate-form" action="{% url 'home' %}" method="post" >
{% csrf_token %}
<span class="login100-form-title">
{{ username_1 }}
</span>
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: ex@abc.xyz">
<input class="input100" type="text" name="username" placeholder=Username>
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-envelope" aria-hidden="true"></i>
</span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Password is required">
<input class="input100" type="password" name="pass" placeholder="Password">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-lock" aria-hidden="true"></i>
</span>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn">
Login
</button>
</div>
<div class="text-center p-t-136">
<a class="txt2" href="#">
<i class="fa fa-long-arrow-right m-l-5" aria-hidden="true"></i>
</a>
</div>
</form>
</div>
</div>
</div>
<!--===============================================================================================-->
<script src="{%static 'login_page/vendor/jquery/jquery-3.2.1.min.js' %}"></script>
<!--===============================================================================================-->
<script src={%static 'login_page/vendor/bootstrap/js/popper.js' %}"></script>
<script src={%static 'login_page/vendor/bootstrap/js/bootstrap.min.js' %}"></script>
<!--===============================================================================================-->
<script src={%static 'login_page/vendor/select2/select2.min.js' %}"></script>
<!--===============================================================================================-->
<script src={%static 'login_page/vendor/tilt/tilt.jquery.min.js' %}></script>
<script >
$('.js-tilt').tilt({
scale: 1.1
})
</script>
<!--===============================================================================================-->
<script src={%static 'login_page/js/main.js' %}"></script>
</body>
</html>
设置.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'dugwc2(qu!9$til@n(gbeur^5bv76m2l&0j(!muv22k+sf$_9@'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'login_page',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'login.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'login.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
文件夹树: 我的 DJANGO 文件夹树屏幕截图
我想要做的是用户名输入并将它们重定向到 html 中的 {{ username_1 }} 。
更新 1:我将 urls.py 和 html 修改为以下内容,同样的问题。
意见。
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('', views.home, name="contact"),
]
html
<form class="login100-form validate-form" action="{% url 'contact' %}" method="post" >
{% csrf_token %}
<span class="login100-form-title">
{{ username_1 }}
</span>
SahilDesai 提供的解决方案对我有用
解决方案
login_page/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('contact/', views.contact, name="contact") # Registering the contact function
]
里面的html文件
<form class="login100-form validate-form" action="{% url 'contact' %}" method="post" >
在表单标记内传递联系人函数的名称参数
推荐阅读
- c - OSDev - 在“docker”中使用“make”命令时出错
- visual-studio - omptarget.dll 未找到
- python - 另一个 pandas .drop 内存泄漏
- hive - Hive 是否需要显式命令才能将数据从 HDFS 加载到表中
- python - 从响应对象获取文件内容
- r - 是否可以使用 Wilcoxon 检验比较不同子组内的组前后,用 gtsummary 生成 gtsummary 表?
- android - Activity/Fragment之间类似IOS的Android垂直过渡动画
- r - 将 NA 替换为 R 中的行均值作为子集
- electron - 快速图像流到画布
- javascript - 角度嵌套对象绑定在按钮 onclick 内不起作用