0

我想要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 个类,名称为:“用户名”和“密码”。请大家帮帮我:(

非常感谢您的帮助:)

4

2 回答 2

1

好的,我自己找到了答案...令人沮丧的是我浪费了这么多时间,但我在 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>

我像这样改变了我的views.py:

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')
于 2013-10-25T10:34:15.363 回答
1

好吧,当浏览器第一次得到要填写的表单时,您正在发送一个GET请求。否则,如果您向服务器发送信息则需要发送POST请求。看看文档

于 2013-10-24T11:58:25.153 回答