Django evolution 
http://code.google.com/p/django-evolution/

When you run ./manage.py syncdb, Django will look for any new models that have been defined, and add a database table to represent those new models. However, if you make a change to an existing model, ./manage.py syncdb will not make any changes to the database.

This is where Django Evolution fits in. Django Evolution is an extension to Django that allows you to track changes in your models over time, and to update the database to reflect those changes.


[ add comment ] ( 5 views )   |  [ 0 trackbacks ]   |  permalink
Django Tutorial: A Simple Blog, Part  
http://lightbird.net/dbe/blog.html

[ add comment ] ( 5 views )   |  [ 0 trackbacks ]   |  permalink
python: django - authenticate user (logoff code) 

def logout_user( request ):

logout( request )

return HttpResponseRedirect('/which_user/')



[ add comment ] ( 5 views )   |  [ 0 trackbacks ]   |  permalink
python: django - authenticate user (logon code) 

def authenticate_user( request ):

if request.method == 'POST':

username = request.POST['username']
password = request.POST['password']

user = authenticate( username = username, password = password )

if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/which_user/')

else:
return HttpResponseRedirect('/which_user/')

else:
return HttpResponse( "non existing user" )

else:
return render( request, 'login.html' )

def logout_user( request ):
logout( request )
return HttpResponseRedirect('/which_user/')



html login code

Log in
{% csrf_token %}




[ add comment ] ( 5 views )   |  [ 0 trackbacks ]   |  permalink
python: django - show elements in class via template 
views.py


from django.shortcuts import render

from app.models import someclass

def show_all_objects_in_class( request ):
all_objects_in_class = someclass.objects.all()
return render( request, 'template.html', { 'all_objects_in_class': all_objects_in_class } )



template

<h1>list of objects</h1>
{% for object in all_objects_in_class %}

<ul>
<div class="attr1">{{ object.attribute1 }}</div>
</ul>
<ul>
<div class="attr2">{{ object.attribute2 }}</div>
</ul>
<ul>
<div class="attr3">{{ object.attribute3 }}</div>
</ul>
{% endfor %}



[ add comment ] ( 5 views )   |  [ 0 trackbacks ]   |  permalink
python: django - create model with custom user rights (app/models.py) 

class MyModel( models.Model ):
comment = models.CharField( max_length = 200 )

def __unicode__(self):
return self.comment

class Meta:
permissions = (
("edit_mymodel", "If you got it, you may can Edit MyModel objects"),
)



[ add comment ] ( 4 views )   |  [ 0 trackbacks ]   |  permalink
python: django - test if user has specific rights on app.object 
1 import functionality

>>> from django.contrib.auth.models import User



2 get the user

>> user = User.objects.get( username = "test" )


3 list all the User class fields

>>> dir(user)
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_base_manager', '_default_manager', '_deferred', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', '_set_pk_val', '_state', 'check_password', 'clean', 'clean_fields', 'date_error_message', 'date_joined', 'delete', 'email', 'email_user', 'first_name', 'full_clean', 'get_absolute_url', 'get_all_permissions', 'get_full_name', 'get_group_permissions', 'get_next_by_date_joined', 'get_next_by_last_login', 'get_previous_by_date_joined', 'get_previous_by_last_login', 'get_profile', 'groups', 'has_module_perm s', 'has_perm', 'has_perms', 'has_usable_password', 'id', 'is_active', 'is_anonymous', 'is_authenticated', 'is_staff', 'is_superuser', 'last_login', 'last_name', 'logentry_set', 'natural_key', 'objects', 'password', 'pk', 'prepare_database_save', 'save', 'save_base', 'serializable_value', 'set_password', 'set_unusable_password', 'unique_error_message', 'user_permissions', 'username', 'validate_unique']
>>> user.__dict__
{'username': u'admin', 'first_name': u'', 'last_name': u'', 'is_active': True, '_state': <django.db.models.base.ModelState object at 0x13116d0>, 'email': u'admin@foo.com', 'is_superuser': True, 'is_staff': True, 'last_login': datetime.datetime(2012, 11, 9, 12, 8, 38, 470670, tzinfo=<UTC>), 'password': u'pbkdf2_sha256$10000$ohDKZdigVWsm$uJ2E08jkCT1fjFTm28bJpDUcQ3V3zjm9gTQ2TmioTEM=', 'id': 1, 'date_joined': datetime.datetime(2012, 11, 8, 13, 39, 27, 353787, tzinfo=<UTC>)}


4 test if user has certain rights

>>> user.has_perm( 'res.edit_itypes' )
True/False


related: https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.models.User


[ add comment ] ( 4 views )   |  [ 0 trackbacks ]   |  permalink
python: django - check if user is authenticated and has certain permission 

def which_user( request ):

try:

user = User.objects.get( username = request.user )

except:

out = "does not exist"

return HttpResponse( out )

if user.is_authenticated():

out = "user is authenticated and his username is ( %s )" % ( user )

if user.has_perm( 'res.edit_itypes' ):

out = "%s and has permission you check for" % ( out )

else:

out = "%s and has no permission you require" % ( out )

else:

out = "user is not authenticated"

return HttpResponse( out )



[ add comment ] ( 4 views )   |  [ 0 trackbacks ]   |  permalink
python: editable content via object 

# Create your views here.
from django.http import HttpResponse
from django.template import Context, Template from django.shortcuts import render_to_response from django import forms from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseRedirect

from forms import LogTypeForm
from models import LogType

def new_logtype(request):


if request.method == 'POST':
form = LogTypeForm( request.POST )
if form.is_valid():
#form = LogTypeForm( request.POST, instance=LogType )
#print str(form)
print "Saving form..."
logtype = form.save()
print logtype
print logtype.id
print dir(logtype)
else:
form = LogTypeForm()
print str(form)

return render(request, 'logcheck_index.html', {'form': form})

def edit_logtype(request, logtype_id):
logtype_model = get_object_or_404(LogType, id=logtype_id)

if request.method == 'POST':
form = LogTypeForm( request.POST, instance=logtype_model )
if form.is_valid():
#form = LogTypeForm( request.POST, instance=LogType )
#print str(form)
print "Saving form..."
logtype = form.save()
print logtype
print logtype.id
print dir(logtype)
else:
form = LogTypeForm(instance=logtype_model)
print str(form)
return render(request, 'logcheck_index.html', {'form': form})



[ add comment ] ( 4 views )   |  [ 0 trackbacks ]   |  permalink
python: django - creating forms from models 
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/

[ add comment ] ( 3 views )   |  [ 0 trackbacks ]   |  permalink

<<First <Back | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | Next> Last>>