繁体   English   中英

如何在django中提交数据?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM