2013-10-24 9 views
0

3 페이지를 갖고 싶습니다. 하나는 특정 디렉토리의 특정 사용자 이름을 "정적"이라고 표시합니다 (내 views.py에서 볼 수 있습니다). 사용자가 사용자를 추가하려면 2 개의 "추가"버튼 중 하나를 누릅니다. 사용자가 사용자 이름과 비밀번호를 입력 할 수있는 두 번째 페이지로 이동하면 "제출 "버튼을 누릅니다. 그런 다음 데이터를 "정적"폴더에 저장해야합니다. 그 후 그는 "등록 성공"이라고 말한 다른 사이트로 이동하고 3 초 후 index.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> 

그리고 이것은 t의 템플릿입니다 그는 사이트를 리디렉션 :

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

하지만 난 정말 '돈 :

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

내가이 줄은 도움이 될 수 있다고 생각 : 나는 문서를 읽고 나는이 코드 예제를 발견

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

이 프로젝트를 내 프로젝트에 할당하는 방법을 알았습니다./이미 "Username"과 "Password"라는 이름으로 모델에 2 개의 클래스를 만들었습니다. 사람은

도움이 많이 감사합니다 :(저를 도와주세요 :

답변

1

좋아요. 답을 직접 찾았습니다 ... 너무 많은 시간을 낭비했지만, 제출자 버튼 주변에 adduser.html (두 번째 페이지)에 장님이 표시되었습니다. .. 제출 된 유일한 것은 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') 
1

글쎄, 브라우저가 먼저 채우기 위해 양식을 얻을 때, 당신은 GET 요청을 전송하고 있습니다. 그렇지 않은 경우 서버 으로 정보를 보내면 POST 요청을 보내야합니다. documentation을 살펴보십시오.

+0

잘 그래 나는 문서에보고를했다 및 I post 메서드로 시도해 보았지만 분명히 작동하지 않았고 실제로 어떻게해야할지 모르겠다. / –

관련 문제