我的Python程序addName旨在接受3个变量(名字,姓氏和性别)。如何从Django Views调用它并将这些变量传递给我的Python程序?它也与views.py
位于同一目录中我会看到" print"导致我执行的同一终端" runserver"或者我必须使用某种类型的stdout重定向输出?
**addName.py**
def webform (firstname, lastname, gender):
print (firstname, lastname, gender)
**views.py**
from django.shortcuts import render
#from django.http import HttpResponseRedirect
from userinfo.forms import UserInfo
from addName import webform
# Create your views here.
def addname (request):
if request.method == 'POST':
form = UserInfo (request.POST)
if form.is_valid ():
data = form.cleaned_data
firstname = data ['first_name']
lastname = data ['last_name']
gender = data ['gender']
context = {'first_name': firstname, 'last_name': lastname, 'gender': gender}
webform (firstname, lastname, gender)
#return HttpResponseRedirect ('/userinfo/results')
return render (request, 'userinfo/results.html', context)
else:
form = UserInfo ()
return render (request, 'userinfo/addname.html', {'form': form,
'first_name': request.POST.get ('first_name', ''),
'last_name': request.POST.get ('last_name', ''),
})
**addname.html**
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Add Name</title>
</head>
<body>
<h1>Add Name</h1>
<form action="" method="POST">{% csrf_token %}
<div class="gender">
<span style="color: red;">{{ form.gender.errors }}</span>
<label for="id_gender_0">Gender:</label>
{{ form.gender }}
</div>
<div class="first_name">
<span style="color: red;">{{ form.first_name.errors }}</span>
<label for="id_first_name">First Name:</label>
{{ form.first_name }}
</div>
<div class="last_name">
<span style="color: red;">{{ form.last_name.errors }}</span>
<label for="id_last_name">Last Name:</label>
{{ form.last_name }}
</div>
<p><input type="submit" value="Add Name" /></p>
</form>
</body>
</html>
**results.html**
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Result</title>
</head>
<body>
<h2>Submitted Entries:</h2>
{{ first_name }} {{ last_name }} {{ gender }}
<p><a href="{% url "addname" %}">Add more names</a></p>
</body>
</html>
**directory**
user@ubuntu:~/Documents/Python/django-project/django-ab/addressbook/userinfo$ ll
total 56
drwxr-xr-x 5 user user 4096 Dec 4 00:16 ./
drwxr-xr-x 6 user user 4096 Nov 24 20:15 ../
-rw-r--r-- 1 user user 107 Dec 3 22:35 addName.py
-rw-r--r-- 1 user user 63 Nov 24 20:15 admin.py
-rw-r--r-- 1 user user 306 Nov 24 22:38 forms.py
-rw-r--r-- 1 user user 0 Nov 24 20:15 __init__.py
drwxr-xr-x 3 user user 4096 Nov 24 23:17 migrations/
-rw-r--r-- 1 user user 57 Nov 24 20:15 models.py
drwxr-xr-x 2 user user 4096 Dec 4 00:16 __pycache__/
drwxr-xr-x 3 user user 4096 Nov 24 20:35 templates/
-rw-r--r-- 1 user user 60 Nov 24 20:15 tests.py
-rw-r--r-- 1 user user 416 Dec 3 21:24 urls.py
-rw-r--r-- 1 user user 986 Dec 4 00:16 views.py
答案 0 :(得分:0)
你会看到&#34; print&#34;导致执行&#34; runserver&#34;的同一终端。为什么不试试呢?
答案 1 :(得分:0)
你可以打电话给你的方法
来自您的观点
喜欢
test_method(f_name,l_name,gendre)
并使用
from filename import test_method
在顶部
是的,它会在终端显示所有的打印声明。
答案 2 :(得分:0)
谢谢大家的意见。我想通了。
我改变了
from addName import webform
到
from userinfo.addName import webform