Get python django model choice in View

Posted In: , . By CreativeSolutions


Suppose you have a model

 class User(models.Model):

   TYPES = (
('Admin', 'Admin'),
('Staff', 'Staff'),
  )


 You can access this in the view using the following code

 In your view file

 from app.core.models import User

 def save(request):
     user_types = User.TYPES

    render('user/form.hrml', {user_types:user_types})

If you want to use this variable in the template, Assign that value user_types as a dictionary variable then use the below code
{% for value, text in user_types %}
{{ value }} {{ text }}
{% endfor %}


 

How to show all the form errors in django form

Posted In: . By Webdevelopmentlogics


{% if my_form.errors %}

div id="errors"
div class="inner"
There were some  errors in the data you entered. Please correct the following
{{ my_form.non_field_errors }}
ul
{% for field in my_form %}
{% if field.errors %}li{{ field.label }}: {{ field.errors|striptags }}/li{% endif %}
{% endfor %}
/ul
/div
/div

{% endif %}

 

Django query with list of values in where clause

Posted In: , . By CreativeSolutions


If we need to fetch data like the MySQL IN clause, we can do it by using filter function with the list of values
user_ids= [1,2,3]
Ex. User.objects.filter(id__in = user_ids)

 


import time

Then add the below code
#Finding the daylight saving time
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = (time.altzone if is_dst else time.timezone)
#Checking for negative and positve offset values
    if(utc_offset > 0):
        utc_offset_hours = (utc_offset / 3600)
        sign = '- '
    else:
        utc_offset_hours = (-(utc_offset) / 3600)
        sign = '+ '
    utc_offset_minutes =  (utc_offset % 3600) / 60
#Converting Integer values to string before concatenating
    utc_offset = sign + str(utc_offset_hours) + '.' +str(utc_offset_minutes)
    print utc_offset

 

Install new python package in virtualenv

Posted In: , . By Webdevelopmentlogics


  1. Enable virtualevn using command 'soure bin/activate'
  2. Then try the command 'echo $PATH' to show it's path. Then it will show the path with like given below
     /var/path/virtauanenv/bin
  3. Go the the package folder which you want to install. Then use the below command
     /home/user/package/folder$ /var/path/virtauanenv/bin/python setup.py install