Answer the question
In order to leave comments, you need to log in
Django. How to save form fields on page reload?
How to restore form fields in case of reloading the screen form by the user (via F5 or by the browser button)
Actually, here is an example of what I'm doing. This is a kind of blog
def post(request):
if request.method=="POST":
title = request.POST.get("title")
text = request.POST.get("text")
author = request.POST.get("author")
if title and text and author:
setpub(title=title,
date=datetime.now().strftime("%d.%m.%Y %H:%M:%S"),
author=author,
text=text)
return redirect("/mytestblog/publication/")
else:
msg={"error1":"Необходимо указать заголовок и текст публикации",
"error2":"Необходимо указать автора публикации",
"title": title,
"author": author,
"datetime":datetime.now().strftime("%d.%m.%Y %H:%M:%S"),
"text": text}
if title and text:
del msg["error1"]
if author:
del msg["error2"]
return render(request, 'post.html',
msg)
return render(request, 'post.html', {
"datetime":datetime.now().strftime("%d.%m.%Y %H:%M:%S")
})
{% extends "base.html" %}
{% block title %}Добавить публикацию{% endblock %}
{% block сontent %}
<h1>Добавить новую публикацию</h1>
<p><b><font color="red">{{ error1 }}</font></b></p>
<form action="/mytestblog/post/" method="post">
{% csrf_token %}
<input style="width: 50%" type="text" name="title" value="{{ title }}" placeholder="Название публикации"/><br /><br />
<textarea style="width: 50%; height: 200px; resize: none"
name="text" placeholder="Текст публикации">{{ text }}</textarea><br />
<p><b><font color="red">{{ error2 }}</font></b></p>
<input style="width: 20%" type="text" name="author" value="{{ author }}" placeholder="Автор публикации"/><br />
<p>{{ datetime }}</p>
<button>Опубликовать</button><br />
</form>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
This is not about django, but about knowing how http works, with F5 the same GET request as when receiving this page, of course, the entered data will not be saved, but if you really need it, you can save it using js, but this also does not have nothing to do with django.
Threat Ask one question at a time. Well, the views code is very bad, does not use very much from django
To save data in the session, including those entered in the input fields, you need JS and sessionStorage\localStorage\cookies and (importantly) it is desirable to clear them when the form is successfully submitted, because the next time you catch the same data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question