helpful links to learn django forms

Posted In: . By CreativeSolutions
 


1. Install virtualenv by dowloading or using the command 'yum install virtualenv' in terminal
 2. Then in the project's folder, run the command
     virtualenv [name] ex: venv
    with this you can add -no-site-packages or other options
 3. Then install django and necessary module in it.
 4. from the 'venv' directory created on executing the command 'virtualenv'
    run the command 'source bin/activate' in terminal
 5. run the command 'deactivate' from terminal to exit

 

How to set and print views variables in template

Posted In: . By CreativeSolutions


In the view
    return render(request, 'admin/user/form.html', {'form': form,'error_msg': error_msg})
In the template
    form action="" method="post">{{ form.as_p }}
    input type="submit" value="Save" /
    /form
    {% if error_msg %}
        {{ error_msg }}
    {% endif %}

 

Common field types in django models

Posted In: . By CreativeSolutions


OneToOneField
    - models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)(SET_NULL, SET_DEFAULT)
ManyToManyField
    - models.ManyToManyField(Group, related_name='profiles', blank=True, null=True)
    - models.ForeignKey(User, related_name='+', on_delete=models.CASCADE) - no backward relation on setting related_name to +
CharField
    - CharField(max_length=100, blank=True, null=True, choices=CARRIER_CHOICES, default=CARRIER_CHOICES[-1][0])
    -
EmailField
    - models.EmailField(max_length=255, blank=True, null=True)
ImageField
    - models.ImageField(blank=True, null=True, upload_to=settings.UPLOAD_ROOT)
DateField
    - models.DateField(blank=True, null=True)
    - models.DateTimeField(auto_now_add=True)  - for saving new date
    - models.DateTimeField(auto_now=True) - for last modified time
    - models.TimeField()
URLField
    - models.URLField(max_length=255, blank=True, null=True)
BooleanField   
    - models.BooleanField(default=True)
IntegerField   
    - models.IntegerField(default=0)   

 

Set not null validation in django form

Posted In: . By CreativeSolutions





To set the not null validation, we need to set the 'Blank=False' in the corresponding field declaration in model
 
  Ex:
      name = models.CharField(max_length=255, blank=False, null=True)
     
      In this null=True refers to the database property