3ページにしたいです。1つは、「静的」と呼ばれる特定のディレクトリの特定のユーザー名を表示します(私のviews.pyに表示されます)。ユーザーがユーザーを追加したい場合は、2 つの [追加] ボタンのいずれかを押します。そうすると、ユーザー名とパスワードを入力できる 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')
これは、最初の Web サイトのテンプレートです。
<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>
そして、これは私の 2 番目の Web サイトのテンプレートです。
<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つのクラスを既に作成しています。みんな私を助けてください:(
助けていただければ幸いです:)