ログインボックスと投稿フォームがあるページを作成するには、次のチュートリアルのリンクに従っています。django、pythonで 'redirect'と 'message'を使う方法
私がここで達成しようとしているのは、サイトにログインしている人だけが投稿フォームに書き込んでコンテンツを送信できるようにすることです。
https://docs.djangoproject.com/en/1.9/topics/auth/default/
セクション「どのようにユーザーをログに記録する」の下には、上記のリンクに行けば、 は、それは基本的に と '「の成功のページにリダイレクト」
を使用するように私に指示します無効なアカウントエラーメッセージを返す 'と '無効なログインエラーメッセージを返す '
ここでの問題は、私がPythonとDjangoについて学んだのはわずか数日であり、そのような機能が必要になるたびに、私はほとんど何も見つけることができませんそれらの機能を学ぶためのソース、またはコードを入手する。
私は既にGoogleで検索し、「リダイレクト」のためにStackOverflowが、私は得ることができた例はこれだった:
@app.route('/success/<int:result_id>')
def success(result_id):
# replace this with a query from whatever database you're using
result = get_result_from_database(result_id)
# access the result in the tempalte, for example {{ result.name }}
return render_template('success.html', result=result)
@app.route('/survey')
def survey():
if request.method == 'POST':
# replace this with an insert into whatever database you're using
result = store_result_in_database(request.args)
return redirect(url_for('success', result_id=result.id))
# don't need to test request.method == 'GET'
return render_template('survey.html')
と私は「成功は」 '内でそこに着いたかを理解するために、まだ午前redirect() '
現在、現在作業中のviews.pyがあります。
from django.contrib import messages
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404, redirect
from.forms import PostForm
from .models import Post
from django.contrib.auth import authenticate, login
def post_detail(request, id=None):
#instance = Post.objects.get(id=1)
instance = get_object_or_404(Post, id=id)
context = {
"title": instance.title,
"instance": instance,
}
return render(request, "post_detail.html", context)
def post_list(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# Redirect to a success page.
else:
# Return a 'disabled account' error message
else:
# Return an 'invalid login' error message.
form = PostForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
print (form.cleaned_data.get("title"))
instance.save()
# message success
messages.success(request, "Successfully Created")
return HttpResponseRedirect(instance.get())
#else:
#messages.error(request, "Not Successfully Created")
queryset = Post.objects.all()#.order_by("-timestamp")
context = {
"object_list": queryset,
"title": "List",
"form": form,
}
return render(request, "post_list.html", context)
#return HttpResponse("<h1>List</h1>")
def post_update(request, id=None):
instance = get_object_or_404(Post, id=id)
form = PostForm(request.POST or None, instance=instance)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
# message success
messages.success(request, "Saved")
return HttpResponseRedirect(instance.get_absolute_url())
context = {
"title": instance.title,
"instance": instance,
"form":form,
}
return render(request, "post_form.html", context)
def post_delete(request, id=None):
instance = get_object_or_404(Post, id=id)
instance.delete()
messages.success(request, "Successfully deleted")
return redirect("posts:list")
私はこれらの種類の機能について助けを得ることができますか?
また、ログインフォームの実装方法を説明するチュートリアルもありますか?
ありがとうございました。
あなたが理解していないこと! Docは、ホームページを表示してホームページをリダイレクトしたり、成功ページをレンダリングしてレンダリングしたりする場合、この場合成功した認証に成功すると成功ページにリダイレクトします。それは何が間違っていますか? –
@RajaSimonは、MultiValueDictKeyError/ –
を含むあらゆる種類のエラーを取得しています。 '@ app.route()'の最初のスニペットは、Djangoではなく、Flaskのようです。 – knbk