Django/HTML - HTML 未显示上下文中的完整字符串,而只是其中的一部分
Django/HTML - HTML not showing complete string from the context, rather only part of it
我正在 Django 网站上工作。此站点有一个带有预览按钮的表单。
我希望它如何工作:
在 HTML 的输入字段中输入的文本。按下预览按钮。 Python 脚本 运行s 在后台。保留其值的形式。
我做了什么:
到目前为止,我已经设法创建了一个表单、一个预览按钮、运行 脚本和 return 值作为 HTML 的上下文并将其显示在表单上。
但是问题是,如果字符串有空格。表单仅显示字符串的第一部分而不是整个字符串。
示例:如果我输入名称为“John Smith”并按下预览按钮。表格只显示“约翰”。如果我做“英国伦敦”,也一样。它显示“伦敦”,
我查看了 Google 和 Whosebug,但找不到解决方案,因此想在这里提问。如果有人可以指导我,我将不胜感激。
views.py
'''
nname = request.POST['name']
django_run_script()
context = {'name': nname}
'''
index.html
'''
<div class="form-group">
<label class="label" for="name">Full Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required value={{ name|default:"" }}>
</div>
'''
谢谢,
亲切的问候,
沙山克
编辑1:
Views.py
# # -*- encoding: utf-8 -*-
# """
# Copyright (c) 2019 - present AppSeed.us
# """
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse, HttpResponseRedirect
from datetime import datetime, timedelta
from django.conf import settings
from django import template
from django.contrib.auth.models import User
from app.script import django_run_script
def index(request):
context = {}
if request.method=='POST' and 'preview' in request.POST:
nname = request.POST['name']
location = request.POST['location']
django_run_script(nname)
print(nname, location)
context = {
'name': nname,
'location': location,
}
return render(request, 'index.html', context)
return render(request, 'index.html', context)
models.py
# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""
from django.db import models
# Create your models here.
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,100,300,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="static/assets/css/style.css">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="wrapper">
<div class="row no-gutters mb-5">
<div class="col-md-6">
<div class="contact-wrap w-100 p-md-5 p-4">
<form action="" method="POST" id="contactForm" name="contactForm" class="contactForm">
{% csrf_token %}
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="label" for="name">Full Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required value={{ name|default:"" }}>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="label" for="name">Select location</label>
{% comment %} <input type="text" class="form-control" name="location" id="location" placeholder="Location"> {% endcomment %}
<input class="form-control" name="location" id="location" type="text" placeholder="Enter your current location" type="text" autocomplete="on" runat="server" required value={{ location|default:"" }}>
</div>
</div>
<div class="col-md-12" hidden>
<div class="form-group">
<label class="label" for="city">Auto Location</label>
<input type="text" class="form-control" name="city" id="city" placeholder="city" required value={{ city|default:"" }}>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input name="preview" type="submit" value="Preview" class="btn btn-primary btn-block">
<div class="submitting"></div>
</div>
</div>
</div>
</form>
</div>
</div>
<div class="col-md-1 d-flex align-items-center">
<div class="contact100-pic js-tilt" data-tilt>
{% comment %} <img src="/static/assets/images/img-01.png" alt="IMG"> {% endcomment %}
</div>
</div>
<div class="col-md-5 d-flex align-items-center">
<div class="contact100-pic js-tilt" data-tilt>
<img src="/static/assets/images/NameNatal380.png" alt="IMG">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="static/assets/js/jquery.min.js"></script>
<script src="static/assets/js/popper.js"></script>
<script src="static/assets/js/bootstrap.min.js"></script>
<script src="static/assets/js/jquery.validate.min.js"></script>
<script src="static/assets/js/google-map.js"></script>
<script src="static/assets/js/main.js"></script>
<!--===============================================================================================-->
<script src="static/assets/vendor/tilt/tilt.jquery.min.js"></script>
<script >
$('.js-tilt').tilt({
scale: 1.1
})
</script>
<!--===============================================================================================-->
</body>
</html>
解决方案:我想我已经找到了解决这个问题的方法。我不得不做一个小改动。请参阅以下代码:
发件人:
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required value={{ name|default:"" }}>
收件人:
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required value="{{ name|default:"" }}">
我正在 Django 网站上工作。此站点有一个带有预览按钮的表单。
我希望它如何工作: 在 HTML 的输入字段中输入的文本。按下预览按钮。 Python 脚本 运行s 在后台。保留其值的形式。
我做了什么: 到目前为止,我已经设法创建了一个表单、一个预览按钮、运行 脚本和 return 值作为 HTML 的上下文并将其显示在表单上。 但是问题是,如果字符串有空格。表单仅显示字符串的第一部分而不是整个字符串。 示例:如果我输入名称为“John Smith”并按下预览按钮。表格只显示“约翰”。如果我做“英国伦敦”,也一样。它显示“伦敦”,
我查看了 Google 和 Whosebug,但找不到解决方案,因此想在这里提问。如果有人可以指导我,我将不胜感激。
views.py
'''
nname = request.POST['name']
django_run_script()
context = {'name': nname}
'''
index.html
'''
<div class="form-group">
<label class="label" for="name">Full Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required value={{ name|default:"" }}>
</div>
'''
谢谢, 亲切的问候,
沙山克
编辑1: Views.py
# # -*- encoding: utf-8 -*-
# """
# Copyright (c) 2019 - present AppSeed.us
# """
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse, HttpResponseRedirect
from datetime import datetime, timedelta
from django.conf import settings
from django import template
from django.contrib.auth.models import User
from app.script import django_run_script
def index(request):
context = {}
if request.method=='POST' and 'preview' in request.POST:
nname = request.POST['name']
location = request.POST['location']
django_run_script(nname)
print(nname, location)
context = {
'name': nname,
'location': location,
}
return render(request, 'index.html', context)
return render(request, 'index.html', context)
models.py
# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""
from django.db import models
# Create your models here.
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,100,300,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="static/assets/css/style.css">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="wrapper">
<div class="row no-gutters mb-5">
<div class="col-md-6">
<div class="contact-wrap w-100 p-md-5 p-4">
<form action="" method="POST" id="contactForm" name="contactForm" class="contactForm">
{% csrf_token %}
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="label" for="name">Full Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required value={{ name|default:"" }}>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="label" for="name">Select location</label>
{% comment %} <input type="text" class="form-control" name="location" id="location" placeholder="Location"> {% endcomment %}
<input class="form-control" name="location" id="location" type="text" placeholder="Enter your current location" type="text" autocomplete="on" runat="server" required value={{ location|default:"" }}>
</div>
</div>
<div class="col-md-12" hidden>
<div class="form-group">
<label class="label" for="city">Auto Location</label>
<input type="text" class="form-control" name="city" id="city" placeholder="city" required value={{ city|default:"" }}>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input name="preview" type="submit" value="Preview" class="btn btn-primary btn-block">
<div class="submitting"></div>
</div>
</div>
</div>
</form>
</div>
</div>
<div class="col-md-1 d-flex align-items-center">
<div class="contact100-pic js-tilt" data-tilt>
{% comment %} <img src="/static/assets/images/img-01.png" alt="IMG"> {% endcomment %}
</div>
</div>
<div class="col-md-5 d-flex align-items-center">
<div class="contact100-pic js-tilt" data-tilt>
<img src="/static/assets/images/NameNatal380.png" alt="IMG">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="static/assets/js/jquery.min.js"></script>
<script src="static/assets/js/popper.js"></script>
<script src="static/assets/js/bootstrap.min.js"></script>
<script src="static/assets/js/jquery.validate.min.js"></script>
<script src="static/assets/js/google-map.js"></script>
<script src="static/assets/js/main.js"></script>
<!--===============================================================================================-->
<script src="static/assets/vendor/tilt/tilt.jquery.min.js"></script>
<script >
$('.js-tilt').tilt({
scale: 1.1
})
</script>
<!--===============================================================================================-->
</body>
</html>
解决方案:我想我已经找到了解决这个问题的方法。我不得不做一个小改动。请参阅以下代码:
发件人:
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required value={{ name|default:"" }}>
收件人:
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required value="{{ name|default:"" }}">