如何在django中提交数据?

时间:2013-10-24 11:50:08

标签: python html django django-templates django-views

我想要3页。一个显示某个目录的某些用户名,称为"静态" (你会在我的views.py中看到它)。如果用户想要添加用户,他会按下2"添加"按钮..如果他这样做,他将进入第二页,他可以输入用户名和密码,并可以通过"提交"按钮。然后数据应该保存在"静态"夹。在那之后,他被引导到另一个网站,在那里它说“#34;注册成功" 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个类,其名称为:&#34; Username&#34;和#34;密码&#34;。请大家帮帮我:(

非常感谢帮助:)

2 个答案:

答案 0 :(得分:1)

好吧,当浏览器首次填写表单时,您就会发送GET请求。否则,如果您将信息发送到服务器,则需要发送POST请求。看看documentation

答案 1 :(得分: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>

我改变了我的观点。像这样:

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')