Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can just set the definition of your cashTexts field to unique in the models definition:</p> <pre><code>def Cashtexts(models.Model): name = CharField(max_length=50, unique = True) </code></pre> <p>Just set the "unique" argument to true for each field you want to be unique and you're done. The way Django's forms API is working, the form fields will take over all the necessary error handling.</p> <hr> <p>With the model fields set to unique where necessary you can have this much easier:</p> <pre><code>def view(request): if request.method == 'POST': form = CashtextsForm(request.POST) """ the next line is going to check uniqueness for each of the models fields where you have set unique = True If the field is not unique, it s field will have an attribute error, which you can then render to the template """ if form.is_valid(): form.save() else: form = CashtextsForm() context = RequestContext(request) # now i pass the form to the template as 'ref_create' context.update( {'ref_create':form} ) return render_to_response('submit.html',context) </code></pre> <p>Then you simply render that form in your template (either with form.as_p(), form.as_table() or with some custom code like this):</p> <pre><code>{% for field in ref_create %} {{ field.label_tag }} {{ field }} {{ field.errors }} {% endfor %} </code></pre> <p>Each field, set to unique in the model definition, which which would not be unique in the database any more if your form is saved, will now have an error message saying something like "A Cashtext with this name allready exists".</p> <hr> <p>Customizing you error messages can be done in two ways: 1: customize the forms (i do not recommend that, until you have better understandings of how Django works) 2: instead of <code>{{ field.errors }}</code> write something like this:</p> <pre><code>{% if field.errors %} The Cashtext field {{field.name}} value should be unique. {% endif %} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload