我想要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 个类,名称为:“用户名”和“密码”。请大家帮帮我:(
非常感谢您的帮助:)