Crypto Coins House News

Here You Find The Latest Today Altcoins, Bitcoins, Blockchain, Cryptocurrency and Litecoin News

How one can return type discipline errors to the rendered mannequin in a unique Django view


I’ve a bootstrap 4 django mannequin (wordcloud.html) which incorporates a type. The shape was initially created in uncooked HTML format in wordcloud.htmland the view of this mannequin fills within the fields. The shape (create_wordcloud_form) makes use of submit to ship the information to a different view (create_wordcloud_view.py). The unique developer didn’t use django’s built-in template / type / view construction, so there isn’t a type.py or type template.

The issue I want to unravel is validating the shape. This have to be a server aspect validation, as one of many fields have to be examined in opposition to one of many databases to keep away from duplicate fields.

Do I refactor all of the mess into the usual django template / type / view idiom utilizing crispy-forms, or is there a manner to make use of what I have already got and return error messages from the view that processes the shape information?

The sector validation code is easy. I do not know the right way to construction the return declaration in case of discipline errors as a result of wordcloud.html is rendered in a unique view.

create_wordcloud_view:

@method_decorator(login_required, title="submit")
class CreateWordCloud(View):  
     
    def submit(self, request):
        logger.debug("CreateWordCloud START")  
        q_dict = request.POST
        logger.debug("q_dict=%s" % q_dict)
        sources = []
        filters = {}
        cloud_name = None
        for key in q_dict:
            logger.debug("key=%s" % key)
            if key == 'csrfmiddlewaretoken':
                proceed
            if key in ["Titles", "Descriptions", "Comments", "Translations", "Text"]:
                sources.append(key)
            elif key == 'wordcloud_name':
                cloud_name = q_dict['wordcloud_name']
            else:
                values = q_dict.getlist(key)
                if len(values) == 1:
                    filters[key] = values[0]
                else:
                    filters[key] = values

        # Want so as to add discipline validation right here
        # 1. Should have a minimum of one supply
        # 2. wordcloud_name should not be within the desk of wordclouds
        # 3. Alert person that no phrases had been discovered for the mixture of sources
        # and filters chosen

        from wordcloud.duties import create_wordcloud_task_3
        create_wordcloud_task_3(None, cloud_name, sources, filters=filters)
        # If the shape information is legitimate and various phrases are returned from
        # create_wordcloud_task_3, then redirect to wordcloud.html
        
        return redirect("/memorabilia/wordcloud.html", everlasting=False)
        
        # If type is invalid or no phrases returned from create_wordcloud_task_3, 
        # return to wordcloud.html with authentic information and acceptable discipline error
        # messages.
        return ????

create_wordcloud_form (an excerpt from wordcloud.html mannequin):

    <div id="wordcloud_form" model="show: none;">
        <type motion="/wordcloud/ajax/create_wordcloud/" class="was-validated" methodology="submit">{% csrf_token %}
            <div class="form-row">
                <div class="col-md-12">
                    <p>First, choose the supply of phrases:</p>
                </div>
            </div>
            <div class="form-row">
                <div class="form-check form-check-inline">
                    <enter class="form-check-input" kind="checkbox" id="source_text" title="Textual content">
                    <label class="form-check-label" for="source_text">Textual content</label>
                </div>
                <div class="form-check form-check-inline">
                    <enter class="form-check-input" kind="checkbox" id="source_translation" title="Translations">
                    <label class="form-check-label" for="source_translation">Translations</label>
                </div>
                <div class="form-check form-check-inline">
                    <enter class="form-check-input" kind="checkbox" id="source_titles" title="Titles">
                    <label class="form-check-label" for="source_titles">Titles</label>
                </div>
                <div class="form-check form-check-inline">
                    <enter class="form-check-input" kind="checkbox" id="source_descriptions" title="Descriptions">
                    <label class="form-check-label" for="source_descriptions">Descriptions</label>
                </div>
                <div class="form-check form-check-inline">
                    <enter class="form-check-input" kind="checkbox" id="source_comments" title="Feedback">
                    <label class="form-check-label" for="source_comments">Feedback</label>
                </div>
                <span>{{ form_sources_required }}
            </div>
            <div class="form-row mt-3">
                <div class="col-md-12">
                    <p>Second, filter paperwork primarily based on the metadata, if desired:</p>
                </div>
            </div>
            
            {% for class in classes %}
                divisibleby:3  %
                <div class="form-row">
                {% endif %}    
                        <div class="col">
                        <label>{{ class.label }}</label>
                        <choose class="selectpicker" title="{{ class.label }}" a number of data-size="10">
                            {% for possibility in class.choices %}
                                <possibility>{{ possibility }}</possibility>
                            {% endfor %}    
                        </choose>
                        </div>
                divisibleby:3 %    
                </div>
                {% endif%}
            {% endfor %}
            
            <div class="form-row mt-3">
                <div class="col-md-12">
                    <div class="form-group">
                      <label for="wordcloud_name">Lastly, give your wordcloud a reputation</label>
                      <enter kind="textual content" title="wordcloud_name" class="form-control" id="wordcloud_name" placeholder="Enter your wordcloud title">
                        <div class="invalid-feedback">{{ form_cloud_name_required }} {{ form_duplicate_cloud_name_error }}</div>
                    </div>
                    
                </div>
            </div>
            
            <div class="form-row mt-3">
                <div class="col-md-12">
                    <button kind="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </type>       
    </div>

A picture of the rendered form:





Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

  • bitcoinBitcoin (BTC) $ 36,530.00
  • ethereumEthereum (ETH) $ 2,232.32
  • tetherTether (USDT) $ 0.997095
  • binance-coinBinance Coin (BNB) $ 337.61