D
D
Dmitry Temnikov2020-06-28 20:50:36
Django
Dmitry Temnikov, 2020-06-28 20:50:36

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

Function code from views.py
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")
        })
Template Code
{% 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 %}

The example clearly shows how the form and function from the view exchange data. If the page is called from the menu, then the base render is in progress. If the form is submitted, then the next step is to catch the POST message and either save or return the data back to the template. Tell me how to do the same thing? What type of message goes to the server on reboot and is it really possible to catch it at all? Don't kick too hard, just started learning django

Question #2. With an asterisk. In base.html there is a link to the "Add Publication" page. How to try to do the same if the user clicked on this link while on the "Add Post" page and ignore this case otherwise?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-06-28
@bacon

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

A
Arseny, 2020-06-28
Matytsyn @ArsenyMatytsyn

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 question

Ask a Question

731 491 924 answers to any question