【问题标题】:How can i submit data in django?我如何在 django 中提交数据?
【发布时间】:2013-10-24 11:50:07
【问题描述】:

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

非常感谢您的帮助:)

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 好吧,是的,我查看了文档,并使用 post 方法进行了尝试,但显然它不起作用,我真的不知道该怎么做:/跨度>
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 2015-09-23
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2015-07-01
      相关资源
      最近更新 更多