简体   繁体   English

Django - 通用 FormView - 将输入传递到模板中

[英]Django - Generic FormView - Passing inputs, into template

Using, Django==2.1.7, and python3.7 - I am really struggling using FormView - see below script.使用 Django==2.1.7 和 python3.7 - 我真的很难使用 FormView - 请参阅下面的脚本。

How can I pass variable search_string submitted via the form and the value within context["new_context_entry"] added via get_context_data - to the success_url, so those values can be rendered in my template on form submission?如何将通过表单提交的变量search_string和通过get_context_data添加的context["new_context_entry"] get_context_data context["new_context_entry"]的值get_context_dataget_context_data ,以便在表单提交时可以在我的模板中呈现这些值?

class ConfigSearchTest(FormView):
    template_name = 'archiver_app/config_view.html'
    form_class = forms.DeviceSearchForm
    success_url = 'archiver/search_results/'



    def form_valid(self,form):
        search_string = form.cleaned_data['search_string']
        return super(ConfigSearchTest,self).form_valid(form)

    def get_context_data(self, **kwargs):          
        context = super().get_context_data(**kwargs)                     
        new_context_entry = "adding additional"
        context["new_context_entry"] = new_context_entry
        return context

You have done enough to pass something to the template already, you don't need to put it in the URL.您已经做了足够多的工作来将某些内容传递给模板,您无需将其放入 URL 中。

my_template.html my_template.html

{{ new_context_entry }}  # should render as 'adding additional'

If you like, you can also pass URL query parameters to the template.如果您愿意,还可以将 URL 查询参数传递给模板。 Test it by typing into your browser URL bar before proceeding further myurl.com?q=testingtesting .在继续myurl.com?q=testingtesting之前,通过在浏览器 URL 栏中键入来测试它。 Something like this should work (there may be some typos as repl.it is playing up):这样的事情应该可以工作(可能有一些错别字,因为 repl.it 正在播放):

my_views.py my_views.py

class ConfigSearchTest(FormView):
    template_name = 'archiver_app/config_view.html'
    form_class = forms.DeviceSearchForm
    success_url = 'archiver/search_results/'

    def get_context_data(self, **kwargs):          
        context = super().get_context_data(**kwargs)                     
        new_context_entry = "adding additional"
        context["new_context_entry"] = new_context_entry
        context["query_params"] = self.request.GET.get("q", "a default") # should be testingtesting
        return context

my_template.html my_template.html

{{ new_context_entry }}  # should render as 'adding additional'
{{ query_params }}  # should render as 'testingtesting'

If you then want to add ?q=testingtesting to a URL when submit is clicked on your form, you can do some magic in get_success_url:如果您想在表单上单击提交时将?q=testingtesting添加到 URL,您可以在 get_success_url 中做一些魔术:

my_views.py my_views.py

class ConfigSearchTest(FormView):
    template_name = 'archiver_app/config_view.html'
    form_class = forms.DeviceSearchForm
    success_url = 'archiver/search_results/'

    def get_success_url(self):          
        # called automatically after form_valid()
        return self.success_url + '?q=' + self.query_string 

    def form_valid(self, form):
        # called when the form is submitted
        self.query_string = form.cleaned_data['query_string']
        return super(SearchFormView, self).form_valid(form)

    def get_context_data(self, **kwargs):   
        # called whenever the pages is rendered       
        context = super().get_context_data(**kwargs)                     
        new_context_entry = "adding additional"
        context["new_context_entry"] = new_context_entry
        context["query_params"] = self.request.GET.get("q", "a default") # should be testingtesting
        return context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM