[Answered ]-Data input from template file is not storing in .txt file in Django Python ( Get method)?

1👍

Perhaps in the form you use form method="post", and in the view you request request.GET.get.

The following code works for me(bboard replace with the name of your folder where the templates are located):

views.py

def form(request):
    return render(request, 'bboard/index3.html')

def write(request):
    p_no = request.GET.get('p_no')
    temp = p_no.__str__()
    with open('test12345.txt', 'w+') as f:
        f.write(temp)
        f.close

    return HttpResponse(f"<h2>Name: {temp}")

index3.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
    <h2>User form</h2>
    <form method="get" action="{% url 'write'%}">
        {% csrf_token %}
        <p>Name:<br> <input name="p_no" /></p>
        <input type="submit" value="Send" />
    </form>
</body>
</html>

urls.py

urlpatterns = [
    path("form/", form),
    path("write/", write, name="write"),
]

Leave a comment