[英]How can i submit data in django?
我想要3頁。 一個顯示某個目錄的某些用戶名,稱為“靜態”(您將在我的views.py中看到它)。 如果用戶想要添加用戶,則按下2個“添加”按鈕中的一個...如果他這樣做,他將進入第二頁,在那里他可以輸入用戶名和密碼,並可以通過“提交”進行確認“按鈕。 然后數據應保存在“靜態”文件夾中。 之后,他被定向到另一個網站,在那里它說“注冊成功”,3秒后他回到index.html查看結果。 在django文檔中,我認為它們只是以另一種方式幾乎相同xP https://docs.djangoproject.com/en/1.5/intro/tutorial04/我只是不知道如何將他們的示例分配給我的項目:/她是我的觀點:
from django.shortcuts import render
import os
def index(request):
os.chdir("/home/ubuntu/newproject/static")
files = []
for file in os.listdir("."):
files.append(file)
return render(request, 'sslcert/index.html', dict(files = files))
def adduser(request):
return render(request, 'sslcert/adduser.html')
def redirect(request):
return render(request, 'sslcert/redirect.html')
這是第一個網站的模板:
<head>
{% block title %}
<h3>
Following users exist in the folder 'static' :
</h3>
{% endblock %}
</head>
<body>
<table border = "0" width = "100%" align = "left">
<tr>
<td align = "right">
<form action = "adduser.html" method = "post">{% csrf_token %}
<input type = "submit" name = "form" style = "width:8%" value = "Add">
</td>
</tr>
{% for file in files %}
<tr>
<td align = "left"> {{ file }} </td>
</tr>
{% endfor %}
<tr>
<td align = "right">
<form action = "adduser.html" method = "post">{% csrf_token %}
<input type = "submit" name = "form" style = "width:8%" value = "Add">
</form>
</td>
</tr>
</table>
</body>
這是我的第二個網站的模板:
<head>
{% block title %}
<h2 align = "middle" >
<u>Add a user</u>
</h2>
{% endblock %}
</head>
<body>
<table border = "0" width = "100%">
<tr>
<td>
<p>Username:</p>
<input type = "text" name = "" value = "" />
</td>
</tr>
<tr>
<td>
<p>Password:</p>
<input type = "password" name = "" value = "" />
</td>
</tr>
<tr>
<td>
<form action = {% url 'sslcert:redirect' %} method = "post"> {%csrf_token %}
<input type = "submit" value = "Submit">
</form>
</td>
</tr>
</table>
</body>
這是重定向網站的模板:
<head>
<meta http-equiv = "refresh" content = "3; URL=http://10.0.3.79:8000/sslcert/">
{% block title %}
<h4>
Registration successful !
</h4>
{% endblock %}
</head>
我閱讀了文檔,我發現了這個代碼示例:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from polls.models import Choice, Question
# ...
def vote(request, question_id):
p = get_object_or_404(Question, pk=question_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
我認為這條線可能會有所幫助:
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
但我真的不知道如何將它分配給我的項目:/我已經在模型中創建了2個類,名稱為“Username”和“Password”。 請大家幫幫我:(
非常感謝幫助:)
好吧,當瀏覽器首先填寫表單時,您正在發送GET
請求。 否則,如果您向服務器發送信息,則需要發送POST
請求。 看一下文檔 。
好吧,我自己找到了答案......令人沮喪的是,我浪費了這么多時間,但我在adduser.html(第二頁)中有點盲目xP我只是在提交按鈕周圍...只有提交的東西是csrf_token。 現在它看起來像這樣,它也提交用戶名和密碼:
<!DOCTYPE html>
<html>
<head>
{% block title %}
<h2 align = "middle" >
<u>Add a user</u>
</h2>
{% endblock %}
</head>
<body>
<form action = "{% url 'sslcert:redirect' %}" method = "post">{% csrf_token %}
<table border = "0" width = "100%">
<tr>
<td>
<p>Username:</p>
<input type = "text" name = "username" value = "" />
</td>
</tr>
<tr>
<td>
<p>Password:</p>
<input type = "password" name = "password" value = "" />
</td>
</tr>
<tr>
<td>
<input type = "submit" value = "Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
我改變了我的觀點。像這樣:
from django.shortcuts import render
import os
def index(request):
os.chdir("/home/ubuntu/newproject/static")
files = []
for file in os.listdir("."):
files.append(file)
return render(request, 'sslcert/index.html', dict(files = files))
def adduser(request):
return render(request, 'sslcert/adduser.html')
def redirect(request):
username = request.POST['username']
password = request.POST['password']
print username
print password
return render(request, 'sslcert/redirect.html')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.