Very simple user input in django

后端 未结 2 1492
一生所求
一生所求 2020-12-05 21:03

My underlying struggle is I am having trouble understanding how django templates, views, and urls are tied together... What is the simplest, bare minimum way to prompt the u

2条回答
  •  情深已故
    2020-12-05 21:29

    For a user input you'll need 2 views - one to display the page with the form and another to process the data. You hook the first view to one url, say "feedback/", and the other one to a url like "feedback/send/". You also need to specify this second url in your form tag.

    ...

    Now in the second view you can obtain the form data and do whatever you want with it.

    def second_view(request):
        if request.method == "POST":
            get_text = request.POST["textfield"]
            # Do whatever you want with the data
    

    Take a look at this page Fun with Forms. It'll give you the basic understanding. I would also advice to work through the whole book. You should use ether GET or POST (GET is probably not secure). Using form is not mandatory, as you can style it and perform all the validation yourself and then pass the data straight to the model.

提交回复
热议问题