简体   繁体   中英

How can i submit data in django?

I want to have 3 pages. The one displays certain usernames of a certain directory which is called "static" (you will see it in my views.py). And if the user wants to add a user, he presses on one of the 2 "Add" buttons.. if he does that he is getting to the second page where he can type in username and password and can confirm it with the "Submit" button. Then the data should be saved in the "static" folder. After that he is getting directed to another site where it says "registration successful" and after 3 seconds he is getting back to the index.html to see the results. In the django documentation, I think they do almost the same just in another way xP https://docs.djangoproject.com/en/1.5/intro/tutorial04/ I just don't get how I can assign their example to my project :/ Her are my views:

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

This is the template for the first website:

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

And this is the template for my second website:

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

And this is the template for the redirect site:

<head>
 <meta http-equiv = "refresh" content = "3; URL=http://10.0.3.79:8000/sslcert/">
 {% block title %}
  <h4>
   Registration successful !
  </h4>
 {% endblock %}
</head>

I read the documentation and I found this code example:

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

I thought that this Line might be helpful:

try:
            selected_choice = p.choice_set.get(pk=request.POST['choice'])

but I really don't know how to assign this to my project :/ I already created 2 classes in the models with the names: "Username" and "Password". Please guys help me out :(

Help is much appreciated :)

Well, when the browser first gets the form to fill up, you're sending a GET request. Otherwise, if you send information to the server, you need to send a POST request. Take a look at the documentation .

Okay I found the answer myself ... It is frustrating that I wasted so much time on this but I was kind of blind xP in my adduser.html (the second page) I had my just around the submit button ... the only thing that got submitted was the csrf_token. Now it looks like this and it submits the Username and the Password as well:

<!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>

And I changed my views.py like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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