Custom form where fields in Form do not match Model

2008-03-06 Thread Michael Lake

Hi all

I'm having problems in saving a custom form. The form presents list of
choices for a
user. In the model the choices are stored single string field
'choices'. But in my
form I wish to present a list of choices and concatenate them together
to save into
the model.
- Hence the model has 'choices'.
- The Form will have choice1, choice2 etc (probably up to several choices).
The code below is simplified with much cruft removed.

In forms.py I have:

class AddDecisionForm(forms.Form):

choice1 = forms.CharField(label='choice1', required=False)
choice2 = forms.CharField(label='choice2', required=False)

enumerated-choices = []
if choice1:
enumerated-choices.append(choice1)
if choice2:
enumerated-choices.append(choice2)
etc

# Include this here so the form has the field that can be saved to
the model.
choices = forms.CharField(initial='1,2', required=False,
widget=forms.HiddenInput)

def save(self):
d = Decision(**self.cleaned_data)
d.choices = str(enumerated_choices)
d.save()

The above basically outlines "what" I want to do but does not work.

In my views.py I have:

if request.method == 'POST':
   if request.POST['submit_action'] == 'Add':
form = AddDecisionForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect("%s/planner/decision/list/" % root)
else:
# TODO

I'm getting an error when I click Add.
"TypeError at /planner/decision/add/
'choice1' is an invalid keyword argument for this function.

I have read over
http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation
and http://www.b-list.org/weblog/2007/nov/23/newforms/
But still having problems working how how to clean this form data. I
understand that I need to probably do some processing in a clean
method to concatenate the choice1 etc values to choices but maybe even
use del self.base_fields['choice1'] etc somehow to remove them from
the form before it validates?
I'm just really confused.

Mike
-- 
Michael Lake
Caver, Linux enthusiast & interested in most things technical.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Creating link to root

2007-09-16 Thread Michael Lake

Florian Lindner wrote:
> Hello,
> a common problem I have is that I have references in my main template like 
> CSS 
> or an background image:
> 
> 
> 
> This template is used within different paths. Therefore I need to have the 
> styles.css availabe in every path the template could be used.

I'm using a custom tag.

I have in all my templates at the top an {% extends base.html %} which contains 
the
DOCTYPE, html header etc. The base.html template is like this:

{% load template_extensions %}



etc...

In my_app/templatetages/ I have template_extensions.py

# My custom tag and filter definitions.
from django.template import Library
import re
register = Library()
def url_root():
 ''' Returns the string for the root URL as set in the settings.py file. '''
 try:
 from django.conf import settings
 except:
 return '- Cannot import settings file -'

 return settings.URL_ROOT


and in settings.py I have:
URL_ROOT = '/path_to/my_app' # Don't include any trailing /


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: ReStructuredText, markdown and other

2007-08-09 Thread Michael Lake

Hi all

I tried out all three for an application that I was writing. This would be used 
by 
non-computer persons for a wiki thing.

* I liked Markdown syntax as it was very readable. It did not have support for 
a 
simple table syntax - for that you use normal HTML. Likewise you can insert 
HTML div 
tags and css in there.
* REST has very good table support but can't take things like  and 
I didn't like the syntax.
* Textiles syntax isn't as 'text like' as Markdown but it's got tables and you 
can 
add any style info after an element like table{border: 1px solid red}

Hence Textile won out over Markdown for me as I absolutely had to have tables.

-
[EMAIL PROTECTED] wrote:
> I'm watching on some light markup languages that can be used in Python
> - what they can and what they can't do. I'm planning to use one in a
> "sandbox" on my sites and more or less in other apps with some extra
> plugins.
> 
> Markdown:
> + simple, easy to understand
> + there is showdown - markdown in JS so online preview :) (http://
> www.attacklab.net/showdown-gui.html)
> - limited syntax (no tables)
> - hard to extend
> 
> ReST:
> + powerfull syntax
> + rather easy to extend
> - not so obvious syntax in some places
> - errors on bad syntax
> 
> Textile:
> + powerfull syntax
> + looks quite good
> ? never used it,
> ? are there some extras ?
> 
> Currently I'm on Markdown + my template filter on top of it buy maybe
> it's better to use python-born ReST? What are your experiences with
> them?
> 
> 
> > 
> 


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Michael Lake

Nis Jørgensen wrote:
> As others have said, you can stuff things into request.session (after
> adding the right middleware incantations).

Using the session middleware looks far better but it will take me a while to 
try that 
out. I'll try and do that next week.

> What is so bad about leaving the url in the browser? I assume the error
> message you display is the "correct" response for that url. Just
> remember to add the correct status code - probably 400,403 or 404. Note
> that if this is a GET url, it shouldn't have side effects, so your
> "main?delete=100" example seems like a bad idea to begin with (unless
> this is the page that shows an "Are you sure ...?" message). If it is a
> POST, there is a lot of reason not to redirect it, since this will make
> it harder for the user to use the back button to fix things.

Yes its a GET that changes the database and I should look at that and change
"main?delete=100" to main/del/100/ or something and make it a POST.
And yes if I make it a POST I hadn't thought about users using the back button 
- 
which the will do :-(

Thanks for this advice, and to Patrick for how he does things.

Mike




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Michael Lake


Ah does this way seem sensible?

> Nis Jørgensen wrote:
>>The argument to HttpResponseRedirect is a url. You seem to be confusing
>>it with a template. 

code 
# some error occurs
return HttpResponseRedirect('error/2/')


def error(request, message):
{
 error_messages = {
'1': 'You dont have access.' ,
'2': 'Server error ...'
 }  
 return render_to_response('error_page.html', {'message':message})
}

That way the user gets a nice clean URL and I can send a specific message into 
the 
template.

Is there some commonly accepted practice here in Django?

Mike





--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Michael Lake

Hi all

Nis Jørgensen wrote:
> The argument to HttpResponseRedirect is a url. You seem to be confusing
> it with a template. 

OK I can do this:

code 
# some error occurs
message = 'You have ... tell admin that '
return HttpResponseRedirect('error/')

and have in views.py

def error(request, message):
{
   return render_to_response('error_page.html', {'message':message})
}

But how to get the message into error() without passing it as a GET?

> If you want to display different content, you need
> to pass a different url or (not recommended) store the data you want to
> display, then display it to the user at the new url

> But if you have the data available, there is no reason to do a redirect.
> Just render the error message etc to the relevant template, then return
> that to the user.

Why I dont want to pass it like this ?message='You have ... tell admin that 
'
is that its long and if the error is something like main?delete=100 but the 
user cant 
delete that id then a Redirect goes to a nice clean valid URL.
A render_to_response leaves the incorrect URL in the browser.

Mike
-- 



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Best way to pass data to a HttpResponseRedirect?

2007-07-27 Thread Michael Lake

Hi all

A really simple question:

I'm mostly using code like this:
data = {'message': 'Some message to user...'}
return render_to_response('main.html', data)

But for error messages to users if one wishes to use a redirect like this:
return HttpResponseRedirect('/error_page.html')

How can one add data into this page? The Redirect is nice in that it does not 
show 
the old URL which might be quite wrong - hence the error.

The Django docs say that HttpResponseRedirect('/whatever/url/') takes just one 
argument. I'd like to use one error_page.html template for a range of errors.

Do people use maybe HttpResponseRedirect('error/') where error is some defined 
function and somehow pass a message string to it?

Mike





--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Using Threadlocals: Error binding parameter 5 - probably unsupported type.

2007-07-23 Thread Michael Lake

Hi all

I have solved the problem I described below. I just guessed at the solution 
after 
reading Ticket #3413 (closed: invalid). In that case a class object was being 
passed 
as a param to the database instead of a user id. And indeed clue was the full 
error 
message in the traceback 

/usr/lib/python2.3/site-packages/django/db/backends/sqlite3/base.py in execute
params: [, 14]
query : 'UPDATE "lab_data" SET "who"=? WHERE "id"=?' self   


I therefore have changed:
self.who = threadlocals.get_current_user()
to:
self.who = str(threadlocals.get_current_user())

and now I get the usernmame updated correctly and no errors.

I am using Django 0.97-pre, SVN 5754

Michael Lake wrote:
> Hi all
> 
> I am using ThreadLocals from 
> http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
> 
> I am getting this error on submitting the form:
> InterfaceError at /erun/10/1/
> Error binding parameter 5 - probably unsupported type.
> 
> This is my model:
> 
> class Data(models.Model):
>  date_added  = models.DateTimeField('Date added', editable=False)
>  date_changed= models.DateTimeField('Date changed', editable=False)
>  who = models.CharField('Who', maxlength='100', 
> editable=False)
> 
>  def save(self):
>  if not self.id:
>  self.date_added = datetime.datetime.today()
> 
>  self.date_changed = datetime.datetime.today()
>  self.who = threadlocals.get_current_user()
> 
>  return super(Data, self).save()
> 
> The Django users groups turns up a few posts about threadlocals but I can't 
> see 
> anything like the problem that I have here.
> 


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Using Threadlocals: Error binding parameter 5 - probably unsupported type.

2007-07-18 Thread Michael Lake

Hi all

I am using ThreadLocals from 
http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

I am getting this error on submitting the form:
InterfaceError at /erun/10/1/
Error binding parameter 5 - probably unsupported type.

This is my model:

class Data(models.Model):
 date_added  = models.DateTimeField('Date added', editable=False)
 date_changed= models.DateTimeField('Date changed', editable=False)
 who = models.CharField('Who', maxlength='100', editable=False)

 def save(self):
 if not self.id:
 self.date_added = datetime.datetime.today()

 self.date_changed = datetime.datetime.today()
 self.who = threadlocals.get_current_user()

 return super(Data, self).save()

The Django users groups turns up a few posts about threadlocals but I can see 
anything like the problem that I have here.

-- 
Mike




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How do I override a model method?

2007-07-02 Thread Michael Lake

Hi

Thanks, I'll try and follow it better by re-reading it in conjuction with my 
Python 
books. But aleady it's changed my view of Python. Instead of visualising a 
large 
thick Python snake I now see a writing mass of pythons, Medusa like, as I 
concentrate 
  on my Django project :-)

 "Near them their sisters three, the Gorgons, winged
  With snakes for hair-- hated of mortal man--"
  Aeschylus

Malcolm Tredinnick wrote:
> On Tue, 2007-07-03 at 13:02 +1000, Michael Lake wrote:
> [...]
> 
>>I didn't realise I was dealing with both attributes and methods and they were 
>>different. I see the Django docs say "Each field is specified as a class 
>>attribute, 
>>and each attribute maps to a database column." I'll have to do some more 
>>reading on 
>>methods and attributes.
> 
> 
> Just to confuse the issue even more (but it might help in your
> background reading): attributes are just a "names assigned to things" in
> a Python object. The things can be ints, strings, other classes,
> functions or methods. So don't get stuck on thinking that methods and
> attributes or disjoint classes.
> 
> A method is one type of Python object (a function that is given
> information about the instance it acts on, to be very loose with terms).
> An attribute is the name given to any Python object when it is part of
> another object so that you can refer to it later. The same method can be
> referred to by different names (i.e. appear as multiple attributes) for
> example. It's not uncommon to see something like
> 
> __contains__ = has_key
> 
> in a class that has already defined the has_key() method. Now the same
> method (code) is assigned to both attributes (names).
> 
> Regards,
> Malcolm
> 


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How do I override a model method?

2007-07-02 Thread Michael Lake

Hi

> On Tue, 2007-07-03 at 11:35 +1000, Michael Lake wrote:
>>I'm having problems overriding a method in a model.
>>I have seen how to override the save and delete methods and overriding a save 
>>works 
>>fine http://www.djangoproject.com/documentation/model-api/
>>But I'm trying to override other automatically generated methods.
>>
>>Currently if I create a procedure object 'p' I can get its description (a 
>>text field) 
>>via p.description but I want to do some munging of the description before it 
>>is sent 
>>to any of the views that use it.
>>
>>class Procedure(models.Model):
>> ''' Stores information about a procedure.
>>
>> name = models.CharField(maxlength=255, blank=True )
>> description = models.TextField()
>>
>> # Override the automatically generated description method.

Malcolm Tredinnick wrote:
> There is no "description" method automatically defined. There is a
> "description" attribute that will hold a Python string in this
> particular case, but it isn't a method (method == bound function).
> 
>> def description(self):
> 
> Having two attributes called 'description' (your method here and the
> earlier models.TextField object) is not going to work at all. The second
> one will completely override the first definition, since they are
> defined in the same scope. So whatever problems there might be here, the
> train has already run off the rails.

I didn't realise I was dealing with both attributes and methods and they were 
different. I see the Django docs say "Each field is specified as a class 
attribute, 
and each attribute maps to a database column." I'll have to do some more 
reading on 
methods and attributes.

.
> What you can do, though, is define a utility method that is what you
> will access during views (instead of calling the field directly). So
> maybe call your TextField _description and then define a description()
> method (even as a property if you don't want to call it as a method all
> the time) that does whatever munging you want.

What I have now in models is a 'def description_munged(self)' METHOD and it 
uses 
p.description ATTRIBUTE to get the string which it then munges and returns the 
new 
string. If then in my views I call the METHOD p.description_munged() it all 
works.

I can see that if I use p.description_munged i.e. as an attrinbute I get 
nothing but 
calling p.description_munged() as a function works. Its easy to forget what's 
an 
attribute and a method.

Thanks. Much appreciated.

-- 
Michael




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How do I override a model method?

2007-07-02 Thread Michael Lake

Hi all

I'm having problems overriding a method in a model.
I have seen how to override the save and delete methods and overriding a save 
works 
fine http://www.djangoproject.com/documentation/model-api/
But I'm trying to override other automatically generated methods.

Currently if I create a procedure object 'p' I can get its description (a text 
field) 
via p.description but I want to do some munging of the description before it is 
sent 
to any of the views that use it.

class Procedure(models.Model):
 ''' Stores information about a procedure.

 name = models.CharField(maxlength=255, blank=True )
 description = models.TextField()

 # Override the automatically generated description method.
 def description(self):
 lines = self.description.split('\n')
new_description = ''
 for line in lines:
 # do something if a line matches a pattern
 # and append to new_description

 return new_description # <-- TODO this is what I want to return
 # return super(Procedure, self).description() # I tried this
 # return self.description()   # I tried this


What happens is that if 'def description' is defined I get nothing returned 
from 
p.description - and no errors. It appears as if the description method is not 
called 
at all.

-- 
Mike




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Confused on how the groups feature works for the Admin interface

2007-06-07 Thread Michael Lake

Hi all

I'm confused on how the groups feature works for the Admin interface.

When I edit a user I can see at the bottom of the page the Groups section with 
a list 
of groups that I have added via the add group function. I can see the two 
groups 
listed in the list; in my case it's honours & postgrads. I can select both and 
then 
click save but nothing happens. There is no indication that the user belongs to 
a 
group. I thought one would have a box like the Available and Chosen user 
permissions 
where one could copy across groups. But all there is is just one box listing 
the groups.

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: {{ perms }} seems to be empty

2007-05-23 Thread Michael Lake

Jeremy Dunck wrote:
> On 5/23/07, Michael Lake <[EMAIL PROTECTED]> wrote:
>>I notice it's a Set object comprising a single list. So I thought I could do 
>>this:
>>
>>{% for item in perms.lab %}
>>{{ item }}
>>{% endfor %}
> 
> 
> Yeah, you can't do attribute access like that for a set.  It's not the
> same as a dictionary.
> I guess you're looking for any permissions that are for the 'lab' app?

Oh I'll probably just be doing stuff like:

{% if perms.lab.add_experiment %}
< a href="something">Click to add experiment.
{% endif %}
{% if perms.lab.delete_experiment %}
< a href="something">Click to delete experiment.
{% endif %}
{% if perms.lab.change_experiment %}
< a href="something">Click to edit experiment.
{% endif %}

Also I should ask:
return render_to_response('lab/user.html', data)

was replaced with:
ctx = RequestContext(request,data)
return render_to_response('lab/user.html', context_instance=ctx)

But why? What's happening in the later case so why did this fix my problem.
I suppose I'm asking for an explanation of the magic under the carpet when I'm 
not 
even a magicians apprentice and will have problems understanding the answer :-)

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: {{ perms }} seems to be empty

2007-05-23 Thread Michael Lake

Jeremy Dunck wrote:
> Should be:
> 
> from django.template import RequestContext
> ctx = RequestContext(request, data)
> return render_to_response('lab/user.html', context_instance=ctx)

Ah ha :-) Now it works and I get...

perms={% if perms.lab %}
  yes perms {{ perms.lab }}
{% else %}
  no perms
{% endif %}

Now gives...

perms= yes perms
Set(['lab.change_test', 'lab.add_test', 'lab.add_experiment', 
'lab.delete_experiment', 'lab.delete_test', 'lab.change_experiment'])

Thanks Jeremy, Martin & Aidas for your help.

I notice it's a Set object comprising a single list. So I thought I could do 
this:

{% for item in perms.lab %}
{{ item }}
{% endfor %}

But the above just seems to hand the browser and waits and waits...


Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: {{ perms }} seems to be empty

2007-05-23 Thread Michael Lake

Hi all

Martin Hsu suggested to make sure that this is included so I now have this in 
settings.py

TEMPLATE_CONTEXT_PROCESSORS=
("django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n")

Still {{ perms }} is empty or at least not displayed.

Jeremy Dunck wrote:
> You must also render the template using RequestContext rather than
> Context in order for TEMPLATE_CONTEXT_PROCESSORS to be run.
> How is 'request' getting into your template context?
> Show us the view. :)

Here is the view:

def user(request):
 ''' Provides information on the currently logged in user. '''

 message = ''

 # This is an object that represents the currently logged-in user.
 # If the user isn't logged in, this will instead be an AnonymousUser object
 user = request.user

 # How to tell if a user is logged in.
 if request.user.is_authenticated():
 # Do something for authenticated users.
 debug = 'User is authenticated.'
 else:
 # Do something for anonymous users.
 debug = 'User is not authenticated.'
 return HttpResponseRedirect("/login/")

 # now user is authenticated, and has name == request.user.username

 message = 'This user '
 if user.has_perm('lab.add_test'):
 message = message + 'can add a lab test.'
 else:
 message = message + 'cannot add any lab tests.'

 if user.is_authenticated():
 permissions = user.get_all_permissions()
 else:
 permissions = ''

 data = {'request': request,
 'user': user,
 # passing through permissions is just for testing.
 # I should just use {{ perms }} in the template
 'permissions': permissions,
 'message': message,
 'debug': debug,
 }

 return render_to_response('lab/user.html', data)


Here is part of the template user.html

Permissions:
{% for permission in permissions %}
 + {{ permission }} 
{% endfor %}


perms={% if perms.lab %}
 yes perms
{% else %}
 no perms
{% endif %}

{{ message }}

-
And here is the output I get:

Permissions:
+ lab.change_test
+ lab.add_test
+ lab.add_experiment
+ lab.delete_experiment
+ lab.delete_test
+ lab.change_experiment
perms= no perms
This user can add a lab test.


I could create a dictionary in the view of all the permission things I'd need 
in a 
template and pass that in but the {{ perms }} seems like it is really what I 
should 
be using as it supposed to be automatically available from the request object.

-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



{{ perms }} seems to be empty

2007-05-22 Thread Michael Lake

Hi all

I have a template and request is being passed to it and so I expected that
{{ perms }} would be availablke as in Chap 12 of the Django book.

I have this:

Permissions:
{% for permission in in request.user.get_all_permissions %}
+ {{ permission }}
{% endfor %}

{% if perms.lab %}
yes you have permissions
{% else %}
no permissions
{% endif %}

On output I get:

Permissions:
   + lab.change_test
   + lab.delete_test
   + lab.add_test
no permissions


I get the same "no permissions" if I try {% if perms.lab %} or {% if perms %}

What am I doing wrong?

Mike

-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: rendering files in markdown

2007-05-17 Thread Michael Lake

James Bennett wrote:
> On 5/17/07, Milan Andric <[EMAIL PROTECTED]> wrote:
> 
>>So how would i extend the markdown filter?
>>Would this be better handled in another place, better way or different
>>module?
> 
> Shameless self-promotion: I wrote a little app a while back which is
> designed to make this sort of thing much easier; check out the
> documentation here and see if it fits with what you want:
> 
> http://code.google.com/p/django-template-utils/wiki/GenericMarkup

Neat! That will be useful to me as I have been alternating between
{{ description | markdown | wikiform }} and
{{ description | textile | wikiform }} by editing all the templates :-)

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: rendering files in markdown

2007-05-17 Thread Michael Lake

Milan Andric wrote:
> Hello,
> 
> I'm using markdown as an output filter in my templates.
> 
> I'd like to extend it somehow so that it can parse a ![][123] image
> reference but the reference number will be a file object id or a
> inline reference, etc.  So then the object would get rendered
> appropriately and dynamically from the db.
> 
> So how would i extend the markdown filter?

I have done a similar thing. I wanted the user to be able to use
[[ name=concentration value=10 ]] in a wiki to get a text entry box on the HTML 
page
like 

I used a template filter.

In my HTML file I have:

{% load wiki_extensions %}
blah blah ...
{{ description | markdown | wikiform }}

where description is the field name that contains the wiki text.

In app/templatetags/ I have a file wiki_extensions.py

# My custom tag and filter definitions.

import re
from django.template import Library

register = Library()

def wikiform(content):
 '''
 Extends the Wiki syntax by providing a wiki input text box for forms.

 wiki_input is something like this:
 [[ name=speed value='5.0' ]]

 The HTML output will be this:
 

 Standard Input Attributes defined by www.w3.org:

 name:  name of the control to send back to server.  REQUIRED
 type:  type of control to create.
 value: initial value of the control.
 size:  tells the user agent the initial width of the control in
characters.
 maxlength: the maximum number of characters the user may enter.

 (only name is a required attribute)
 '''

 pattern_inputbox = re.compile(r'\[\[.*\]\]') # pattern matches [[ anything 
]]
 pattern_attribute = re.compile(r'\w+=\w+')   # pattern matches 'word=value'
instances

 # Find all attributes for the input text field and get a list of matches.
 input_boxes = pattern_inputbox.findall(content)

 for input_box in input_boxes:
 data = ''
 attributes = pattern_attribute.findall(input_box)
 for attribute in attributes:
 (the_attribute, the_value) = attribute.split('=')
 data = data + "%s='%s' " % (the_attribute, the_value)

 replacement = '' % data
 content = pattern_inputbox.sub(replacement, content, 1)

 return content

# If you are not using Python 2.4 i.e. your using 2.3 then place the
# register.filter lines here after the defines above, otherwise you
# will get a NameError.
register.filter('wikiform', wikiform)



-- 
Michael Lake





--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Newform and Minimal File Upload Example

2007-05-15 Thread Michael Lake

Hi

I'm still trying to find a minimal example.
This http://www.djangosnippets.org/snippets/103/ is very specific to a zip file 
type.
A search of the Django Users Group using 'newforms upload' gives a few posts:
 newforms and FileInput
 Newform and File Upload problem
 "'BoundField' object is unsubscriptable " error when trying to upload file
 validating using newforms
and many of these deal with problems in trying to either do or validate image 
uploads. All seem to have problems and use very different methods.

I'd like to get a min example up and running and then add some validation (like 
checking that it is an image and say checking its size) after I have a small 
test 
working fine.

Michael Lake wrote:
> Hi all
> 
> Guillaume wrote in May "Newform and File Upload problem" 
> 
> 
>>Well, my message was maybe a little long. I still have not found answers
>>to my problem. To make it short, Is there anywhere a short example of file
>>upload using newform ?
>>The most simple thing with a model with 1 FileField, the smallest
>>template and form associated and the handling code.
>>That would be great, and probably useful for some other django beginers.
> 
> 
> I'm also struggling with newforms to upload an image file(s). I have seen 
> that there
> are a few posts that deal with zipfiles and images but all are sufficiently 
> different
> that I'm more confused.
> 
> It would be really appreciated by myself and probably a few others if a 
> minimal but
> full working example could be posted for a general file upload using newforms.
> I realise there are two cases here: upload file to a subdirectory of 
> MEDIA_ROOT and
> upload to a database field. The latter is what I want to do with images.
> 
> MINIMAL MODEL:
> 
> class Test(models.Model):
>  name = models.CharField(maxlength=255)
>  image = models.ImageField(upload_to='images', blank=True, null=True)
>  # What to do if you wish to upload to the image field itself and not to a
>  # directory images/ ?
> 
>  # def save(): # Do we need a save here?
> 
> The data in this model might be something like:
> 
> ++--+--+
> | id | name | image|
> | 1  | A| some_binary_data | <-- binary data if image is in database
> | 2  | B|  | otherwise text data of an image filename.
> ++--+--+
> 
> Here we want a form that allows us to select a row and upload an image say 
> into that 
> row's image field.
> 
> VIEW:
> 
> def test_fileupload(request):
>  ''' This is used for testing file upload. '''
> 
>  class UploadForm(forms.Form):
>  ''' A Django newforms class that handles uploading files. '''
> 
>  choices = []
>  for test in Test.objects.all():
>  choices.append((test.id, test.name))
> 
>  # The variable choices is now something like: [(1,'A'),(2,'B')]
>  choice = forms.CharField(widget=forms.Select(choices=choices))
>  uploadFile = forms.Field(widget=forms.FileInput)
> 
>  def check_file(self):
>  # Check if the file is an image file.
>  # If it's not do something.
>  pass
> 
>  def save(self):
>  pass
> 
>  form = UploadForm()
> 
>  if (request.POST):
>  # The request.POST does not contain file upload data.
>  # Access instead request.FILES
>  id = request.POST['choice']
>  #filename = request.POST['upload']
>  file_name = request.POST['uploadFile']   # <-- does not work
> 
>  if (request.FILES):
>  file_data = request.FILES['uploadFile']
> 
>  if form.is_valid():
>  form.save()
>  else:
>  form = UploadForm()
> 
>  data = { 'form': form,
>  }
>  return render_to_response('app/test_fileupload.html', data)
> 
> 
> MINIMAL TEMPLATE:
> 
> 
> Select row:  {{ form.choice }}
> Select File: {{ form.uploadFile }} and press Upload.
> 
> 
> 
> Looks like this:
> 
> Select row:  [   ] <-- drop down list to select the row
> Select file: [   ] [Browse] and press upload [Upload]
> 


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Newform and Minimal File Upload Example

2007-05-14 Thread Michael Lake

Hi all

Guillaume wrote in May "Newform and File Upload problem" 

> Well, my message was maybe a little long. I still have not found answers
> to my problem. To make it short, Is there anywhere a short example of file
> upload using newform ?
> The most simple thing with a model with 1 FileField, the smallest
> template and form associated and the handling code.
> That would be great, and probably useful for some other django beginers.

I'm also struggling with newforms to upload an image file(s). I have seen that 
there
are a few posts that deal with zipfiles and images but all are sufficiently 
different
that I'm more confused.

It would be really appreciated by myself and probably a few others if a minimal 
but
full working example could be posted for a general file upload using newforms.
I realise there are two cases here: upload file to a subdirectory of MEDIA_ROOT 
and
upload to a database field. The latter is what I want to do with images.

MINIMAL MODEL:

class Test(models.Model):
 name = models.CharField(maxlength=255)
 image = models.ImageField(upload_to='images', blank=True, null=True)
 # What to do if you wish to upload to the image field itself and not to a
 # directory images/ ?

 # def save(): # Do we need a save here?

The data in this model might be something like:

++--+--+
| id | name | image|
| 1  | A| some_binary_data | <-- binary data if image is in database
| 2  | B|  | otherwise text data of an image filename.
++--+--+

Here we want a form that allows us to select a row and upload an image say into 
that 
row's image field.

VIEW:

def test_fileupload(request):
 ''' This is used for testing file upload. '''

 class UploadForm(forms.Form):
 ''' A Django newforms class that handles uploading files. '''

 choices = []
 for test in Test.objects.all():
 choices.append((test.id, test.name))

 # The variable choices is now something like: [(1,'A'),(2,'B')]
 choice = forms.CharField(widget=forms.Select(choices=choices))
 uploadFile = forms.Field(widget=forms.FileInput)

 def check_file(self):
 # Check if the file is an image file.
 # If it's not do something.
 pass

 def save(self):
 pass

 form = UploadForm()

 if (request.POST):
 # The request.POST does not contain file upload data.
 # Access instead request.FILES
 id = request.POST['choice']
 #filename = request.POST['upload']
 file_name = request.POST['uploadFile']   # <-- does not work

 if (request.FILES):
 file_data = request.FILES['uploadFile']

 if form.is_valid():
 form.save()
 else:
 form = UploadForm()

 data = { 'form': form,
 }
 return render_to_response('app/test_fileupload.html', data)


MINIMAL TEMPLATE:


Select row:  {{ form.choice }}
Select File: {{ form.uploadFile }} and press Upload.



Looks like this:

Select row:  [   ] <-- drop down list to select the row
Select file: [   ] [Browse] and press upload [Upload]



-- 
Mike






--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How can I get the admin interface to not ignore newlines in a text field?

2007-05-10 Thread Michael Lake

Alexander Pugachev wrote:
> Newlines characters do not break lines in HTML. You need  in strings to
> show text in few lines in list view.

Ah :-) Obvious now why it wasn't rendering the text as text with newlines..
Thanks.

-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How can I get the admin interface to not ignore newlines in a text field?

2007-05-09 Thread Michael Lake

Michael Lake wrote:
> Alexander Pugachev wrote:
>>What is the type of the field? It should be TextField?
> 
> Yes it is:
>description = models.TextField(core=True)

To be more precise; the new lines do not show when the admin interface just 
displays 
the field but it does show the newlines when you click on the rows ID and edit 
that row.

>>>Hi all
>>>
>>>I have a content field which has newlines in it. When I do a select from
>>>the database of that field I get the output like this:
>>>line 1
>>>line 2
>>>
>>>But when I view this in the admin interface I get
>>>line 1 line 2
>>>
>>>How can I get the admin interface to not ignore newlines?
>>>
>>>Mike


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How can I get the admin interface to not ignore newlines in a text field?

2007-05-09 Thread Michael Lake

Alexander Pugachev wrote:
> What is the type of the field? It should be TextField?

Yes it is:
   description = models.TextField(core=True)

>>Hi all
>>
>>I have a content field which has newlines in it. When I do a select from
>>the database of that field I get the output like this:
>>line 1
>>line 2
>>
>>But when I view this in the admin interface I get
>>line 1 line 2
>>
>>How can I get the admin interface to not ignore newlines?
>>
>>Mike


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How can I get the admin interface to not ignore newlines in a text field?

2007-05-08 Thread Michael Lake

Hi all

I have a content field which has newlines in it. When I do a select from the 
database 
  of that field I get the output like this:
line 1
line 2

But when I view this in the admin interface I get
line 1 line 2

How can I get the admin interface to not ignore newlines?

Mike




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: In Admininterface: Error: variable not passed into template?

2007-05-07 Thread Michael Lake

Hi all

A while ago I had a problem as described below. I found the solution so am 
posting it 
here in case others have the same prob in future.

The "Error: variable not passed into template?" was appearing where the 
headings for 
the table showing the contents of the Data model should have been - but there 
were no 
headings. So it was the headings i.e. the field names of the model that were 
not 
being passed into the template.
(The text of "Error: variable not passed into template?" comes from 
TEMPLATE_STRING_IF_INVALID in my settings file.)

Notice in my Class I had experiment_id and node_id, particularly they have _id 
at the 
end. This is what caused the problem. If instead of this:
   Class Admin:
   pass

I have:

   Class Admin:
   list_display('id', 'experiment_id', 'node_id', 'content')

Then in admin the Data table show the correct headings and there is no errors.
 ID  Experiment id   Node id   Content

Hence it appears that in a model that if one has fields with _id in their name 
there 
are problems.

Michael Lake wrote:
> Hi all
> 
> In the Admin interface I'm getting the folloing error for a model:
> Error: variable not passed into template?>Data
> 
> class Data(models.Model):
>   experiment_id = models.CharField(maxlength=10)
>   node_id   = models.CharField(maxlength=10)
>   content = models.TextField(blank=True)
> 
>   class Meta:
>   verbose_name_plural = 'Data'
> 
>   class Admin:
>   pass
> 
> The Admin interface is not showing the columns of the table but just a list 
> like this:
>   Data object
>   Data object ...
> 
> I can add a row or delete a row and I can edit the rows using the admin 
> interface.
> I can't see whats wrong with this particular model.
> manage syncdb runs with no errors and manage.py validate says 0 errors.
> In models.py the other models display fine in the Admin interface.
> 
> Mike


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: In Admininterface: Error: variable not passed into template?

2007-04-19 Thread Michael Lake

oggie rob wrote:
> You're missing the __str__ method. Not sure if that is causing the
> "Error" though.

Thanks Rob, I added the __str__ method and all it does is that Admin now shows 
each 
entry as whatever the str method returns. It's useful though so thanks.
But I still get the error Error: variable not passed into template?>Data

> On Apr 18, 10:39 pm, Michael Lake <[EMAIL PROTECTED]> wrote:
> 
>>Hi all
>>
>>In the Admin interface I'm getting the folloing error for a model:
>>Error: variable not passed into template?>Data
>>
>>class Data(models.Model):
>>experiment_id = models.CharField(maxlength=10)
>>node_id   = models.CharField(maxlength=10)
>>content = models.TextField(blank=True)
>>
>>class Meta:
>>verbose_name_plural = 'Data'
>>
>>class Admin:
>>pass
>>
>>The Admin interface is not showing the columns of the table but just a list 
>>like this:
>>Data object
>>Data object ...
>>
>>I can add a row or delete a row and I can edit the rows using the admin 
>>interface.
>>I can't see whats wrong with this particular model.
>>manage syncdb runs with no errors and manage.py validate says 0 errors.
>>In models.py the other models display fine in the Admin interface.
>>
>>Mike
>>--
>>Michael Lake
> 
> 
> 
> > 
> 


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



In Admininterface: Error: variable not passed into template?

2007-04-18 Thread Michael Lake

Hi all

In the Admin interface I'm getting the folloing error for a model:
Error: variable not passed into template?>Data

class Data(models.Model):
experiment_id = models.CharField(maxlength=10)
node_id   = models.CharField(maxlength=10)
content = models.TextField(blank=True)

class Meta:
verbose_name_plural = 'Data'

class Admin:
pass

The Admin interface is not showing the columns of the table but just a list 
like this:
Data object
Data object ...

I can add a row or delete a row and I can edit the rows using the admin 
interface.
I can't see whats wrong with this particular model.
manage syncdb runs with no errors and manage.py validate says 0 errors.
In models.py the other models display fine in the Admin interface.

Mike
-- 
Michael Lake





--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Providing action specific feedback messages to users and HttpResponseRedirects

2007-04-13 Thread Michael Lake

Hi all

I'm having problems working out how to pass messages to a form when using a 
Redirect. 
One example I found on the web of a Contact form set a variable 
message='Contact 
Added' or message='Contact Updated' depending on what happened. This example 
didn't 
use any Redirect after a POST.

Many sites strongly suggest to use a HttpResponseRedirect after a POST to cover 
the 
situation of a user hitting the back button and inadvertently making a double 
POST 
and it also can return one to a default page.

However as you will glean from the code below if I have the 
HttpResponseRedirect then 
the form will only ever show 'Please fill in the details.' The form will never 
display any of the other messages. If I comment out the HttpResponseRedirect 
some of 
the other messages will show.

How do Django users manage to provide action specific feedback messages to 
users on 
form submission? What should I be doing instead of what I have below?

# Default form
ContactForm = forms.form_for_instance(contact)
form = ContactForm()
message = 'Please fill in the details.'

if request.POST:
 if request.POST['submit_action'] == 'Update':
 update_form = ContactForm(request.POST)
 if update_form.is_valid():
update_form.save()
message = 'Update done.'
 else:
 message = 'Form was not valid!'

 else:
 message = 'Some other error message.'

 return HttpResponseRedirect("%s" % request.path)

data = { 'request': request,
'message': message,
'form': form.as_table(),
}
return render_to_response('test_form_for_instance.html', data)


Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Restructured text parsing not showing h1 levels.

2007-04-02 Thread Michael Lake

Hi all

Thanks for this help.

[EMAIL PROTECTED] wrote:
> There is an advanced RestructuredText django plugin I wrote with the
> help of David Goodger:
> https://svn.python.org/conference/django/trunk/pycon/markup/
That code looks useful, thanks. It will take me a while to have a look at it 
and see 
how it work.s

"Chris Moffitt" <[EMAIL PROTECTED]> wrote:
>>This is normal Restructured Text behaviour, although I agree it's a bit
>>weird when you first see it. It's kind of a consequence of a ReST
>>feature combined with us (Django) diving into the middle of the output
>>processing pipeline to extract only what we need.
>
>Ah yes, it is the "normal behavior" but it can be configured.  Take a look
>here -http://docutils.sourceforge.net/docs/user/config.html
>and I think you're specifically looking for this text:
>"doctitle_xform
>
>You'll need to make this change in your /etc/docutils.conf file (or in one
>of the other files it mentions).  Then, it should work for you.
Excellent! I didn't know it was easily configurable. I don't have a 
/etc/docutils.conf as docutils is just in the site-packages directory but now 
knowing 
what to look for I can see where this is set. I modified 
docutils/readers/standalone.py as set the value from 1 to 0 and now a lone H1 
is 
shown :-)

It's probably best to use for me to look at trying Dougs code and Jonathan 
Buchanan's 
suggestion rather than modifying the site-package.

Thanks all :-)

-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Restructured text parsing not showing h1 levels.

2007-04-01 Thread Michael Lake

Hi

> On Mon, 2007-04-02 at 15:24 +1000, Michael Lake wrote:
> 
>>Hi all
>>I have a problem I think when I use the markup package. This provides 
>>markdown, 
>>textile and restructuredtext markups. I have the following in views.py:
>>
>>def testwiki:
>>
>> content = '''
>>Restructured Text Test
>>==

>>'''
>>
>>return render_to_response("app/testwiki.html" , {'content': content})
>>In testwiki.html I have {{ content|restructuredtext}}
>>
>>The page is displayed but there is no heading level 1 i.e. I'm missing 
>>"Restructured 
>>Text Test". If I place any character above the heading like 'a' then the 
>>heading 
>>shows fine. It also works fine if I have a H1 anywhere lower down. But If I 
>>just wish 
>>to have one H1 level Im in problems.

Malcolm Tredinnick wrote:
> This is normal Restructured Text behaviour, although I agree it's a bit
> weird when you first see it. It's kind of a consequence of a ReST
> feature combined with us (Django) diving into the middle of the output
> processing pipeline to extract only what we need.
> 
> There is no specific way to specify the document title or subtitle. So
> ReST uses the initial *unique* heading markup as the document title. The
> restructured text template tag only returns the document "fragment"
> after the markup has been performed. This does include the document
> title, which is why you are "losing" the first heading.
> 
> This is in the ReST documentation
> (http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html )
> under "Document Structure", if you care.
> 
> Not sure what a good workaround would be that is always going to work.

Bugger :-) That will probably wipe it out of use for me.
I'm writing an app where users will be inputting experiment descriptions using 
a wiki 
  hence the use of either markdown, textile or ReST. There will only be one H1 
level.
Markdown is nice but does not have tables (I have a table extension for 
Python-Markdown but it looks comlicated to install), textile uses h1., h2. 
which I 
don't like as much and ReST looked fine.

The users will also be incorporating text entry boxes into the wiki so I have 
written 
a small filter that turns [[name=volts size=40]] into .
It's neat but I wanted the tables to allow users to align the input boxes.

I'll probably have to look at extending Markdown and potter back here and ask 
questions when I get stuck.

Thanks Malcolm.

Mike
-- 
Michael Lake



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Restructured text parsing not showing h1 levels.

2007-04-01 Thread Michael Lake

Hi all

I have a problem I think when I use the markup package. This provides markdown, 
textile and restructuredtext markups. I have the following in views.py:

def testwiki:

 content = '''
Restructured Text Test
==

How de do.

Sub Heading
---

Hi
'''

return render_to_response("app/testwiki.html" , {'content': content})

In testwiki.html I have {{ content|restructuredtext}}

The page is displayed but there is no heading level 1 i.e. I'm missing 
"Restructured 
Text Test". If I place any character above the heading like 'a' then the 
heading 
shows fine. It also works fine if I have a H1 anywhere lower down. But If I 
just wish 
to have one H1 level Im in problems.

Maybe this is a restructured text parsing problem? Has anyone seen this?

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: URL prefix in applications and templates.

2007-03-29 Thread Michael Lake

Jeremy Dunck wrote:
> On 3/29/07, Michael Lake <[EMAIL PROTECTED]> wrote:
> ...
>>MyTag is {{ mytag }} end.
>>
> {% mytag %}
> {{ }} is for variables.  :)

Thanks. With that change it is now working. Although I had read the 
documentation and 
it shows {% %} when I was editing in the HTML file I kept using {{ }} like all 
the 
other {{}}s in that file and didn't realise the difference.

Thanks to Michael, Jeremy, Doug & James for your help.

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: URL prefix in applications and templates.

2007-03-28 Thread Michael Lake

Hi all

Im trying Michael's suggestion quoted below for a custom tag but it's not 
working so 
I have tried a very very minimal one and it's not working either.

In app/templatetags/template_extensions.py I have
   from django.template import Library
   register = Library()

   def mytag():
  return 'This is my tag.'

   mytag = register.simple_tag(mytag)

Im my html file I have {% load template_extensions  %}
and I have
MyTag is {{ mytag }} end.

But I get MyTag is end.
i.e. nothing show.

The template_extensions.py is being loaded cause if I make a deliberate syntax 
error 
in there I get a error message.
I can see from 
http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/templatetags/adminmedia.py
how this is done and its so simple so I can't see whats wrong.
How can I track down the error?

Michael wrote:
> In the mean-time, another method which seems to be used by the admin
> interface is to create your own template tag (using simple_tag).
> 
> If you take a look at the template tag used by the admin interface to
> create the {{ admin_media_prefix }} tag, it might give you some tips:
> 
> For your issue, I think you could copy and modify the code from /
> django_src/django/contrib/admin/templatetags/adminmedia.py to :
> 
> from django.template import Library
> 
> register = Library()
> 
> def url_root():
> """
> Returns the string for the root URL depending on the DEBUG
> setting.
> """
> try:
> from django.conf import settings
> except ImportError:
> return ''
> if settings.DEBUG:
>   return settings.URL_ROOT_DEBUG
> else:
>   return settings.URL_ROOT
> 
> url_root() = register.simple_tag(url_root)
> 
> then your templates should just be able to use {{url_root}}.
> 
Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



URL prefix in applications and templates.

2007-03-27 Thread Michael Lake

Hi all

Like other posts on this group I am trying to work out the best approach for an 
app 
that has a development version at / and a production version at /proteomics/pls/

My templates contain href="/detail/{{ some_variable }}" which works in test but 
I 
dont want to have to change them all to
href="/proteomics/pls/detail/{{ some_variable }}" when I copy updates to 
prooduction.
I'm using in urls.py
   (r'^detail'/  , lab.views.detail)

One solution is to have this:
1. In settings.py a variable URL_ROOT = '/' for testing and
URL_ROOT = '/proteomics/pls' for production
2. In views.py I have 'from django.conf import settings'
3. and for each template use
href="{{ settings.URL_ROOT }}/detail/{{ some_variable }}"

But I don't think importing settings when I just need one URL is the way to do 
things 
correctly and also the Django docs say "Also note that your code should not 
import 
from either global_settings or your own settings file." in 
http://www.djangoproject.com/documentation/settings/

So my question is if some consensus has developed or is there a 'proper' method 
for 
dealing with this?

Notes: A search of this mailing group using the text "url root" shows the 
following 
relevant posts:

global settings
http://groups.google.com/group/django-users/browse_thread/thread/e3416f06e13e87c3/053f939b4f5cdaca?lnk=gst=url+root=2=en#053f939b4f5cdaca

Root URI in templates
http://groups.google.com/group/django-users/browse_thread/thread/3346f3f509bf3bf8/c8595511c8d34c05?lnk=gst=url+root=4=en#

Project URL-prefix
http://groups.google.com/group/django-users/browse_thread/thread/f16d6bcf4d667069/d0c58efec5640d61?lnk=gst=url+root=8=en#

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: I wish for a "less than" and "greater than" in templates

2007-03-21 Thread Michael Lake

limodou wrote:
> You can also use PyIf tag(written by me) to do that.
> http://www.djangosnippets.org/snippets/12/
> And it can support any python expression. So it's more general I think.

And it's short and understandable. I like it too.
Thanks

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: I wish for a "less than" and "greater than" in templates

2007-03-21 Thread Michael Lake

Kenneth Gonsalves wrote:

> On 22-Mar-07, at 9:25 AM, Michael Lake wrote:
>>>write your own tag - someone has done it, you can see it in this  
>>>file:
>>>
>>>http://nrcfosshelpline.in/code/browser/trunk/web/templatetags/
>>>base_utils.py
>>>
>>>class CompareNode near the end
>>
>>But that link above no longer works.

> works now - thanks for the heads up on this
Thanks for that Kenneth.
The code there looks like just what I need.

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Having problems getting contrib.markup to load

2007-03-21 Thread Michael Lake

[EMAIL PROTECTED] wrote:
> Hi Michael,
> Have you added "django.contrib.markup" to your INSTALLED_APPS setting?

Ah :-)
I had placed django.contrib.markup into the MIDDLEWARE_CLASSES instead of the 
INSTALLED_APPS section in settings.py.

I have fixed that and it's now saying the markdown library is not installed 
which is 
much much better. I have to get and install that and then I think I should be 
running 
fine.

Thanks
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



I wish for a "less than" and "greater than" in templates

2007-03-21 Thread Michael Lake

Hi all

I'm wishing for a "less than" and "greater than" in templates.
The mailing list has this on the matter:

On 31-Aug-06
>> Is it possible to make "less than" and "greater than" comparisons in
>> templates?
>> I was trying to do something similar to this, but without success:
>> {% if list|length > max_list_length %}
>> Exceeded by {{ list|length - max_list_length }} items
>> {% endif %}
> 
> write your own tag - someone has done it, you can see it in this file:
> 
> http://nrcfosshelpline.in/code/browser/trunk/web/templatetags/
> base_utils.py
> 
> class CompareNode near the end 

But that link above no longer works.
Does anyone have some code for this?
I looked in Django Snippets and didn't find such a snippet.

Mike
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How do I make a TextArea box in a form bigger?

2007-03-15 Thread Michael Lake

Hi all

>>On 14-Mar-07, at 12:02 PM, Michael Lake wrote:
>>>The textarea is about two lines high only. I want the text entry
>>>box to be much bigger as it will be holding a full page of text ...

Kenneth helped out and suggested this:
>>widgets.TextArea(attrs={'rows':10,'cols':60}) for example

Thanks, but I'm still not sure where to place the above code.
The below didn't work:
   ContactForm = forms.form_for_model(Contact)
   form = ContactForm()
   form.widgets.TextArea(attrs={'rows':10,'cols':60})
I'm guessing as I'm confused :-)

Also [EMAIL PROTECTED] wrote:
 > Or give it an id or class and just set it in the CSS.

That I did work out :-) The textarea element has an id and I was able to set
the CSS style sheet to be textarea#id_itsname {width:90ex; height:40ex}
which keeps the user interface stuff in the CSS.

So I have it working using CSS but not with widgets.

Thanks
Mike




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How do I make a TextArea box in a form bigger?

2007-03-13 Thread Michael Lake

Hi all

I'm having problems trying to understand how to customise an element in a form.
Im using a TextField and I want the TextArea element to be much bigger in size.
 From reading I gather one uses widgets. I found this page but it's quite 
different 
from the code in my model or view. http://www.djangosnippets.org/snippets/35/

I'm wanting something like 
The code below is a minimal example that displays something like this
   ++
   ||
Lots of text: ++

The textarea is about two lines high only. I want the text entry box to be much 
bigger as it will be holding a full page of text including HTML tags.

MODELS

from django.db import models

class Contact(models.Model):
 name  = models.CharField(maxlength=500)
 phone = models.CharField(maxlength=200)
 details = models.TextField()

VIEWS

from contacts_tutorial.contacts.models import Contact
from django import newforms as forms
from django.shortcuts import render_to_response

def test(request):

 ContactForm = forms.form_for_model(Contact)
 form = ContactForm()

 data = { 'request': request,
  'form': form,
 }

 return render_to_response('test.html', data)

TEMPLATE


Lots of text: {{ form.details }}




-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help needed in understanding newforms.

2007-03-01 Thread Michael Lake

Malcolm Tredinnick wrote:
> I fear you might still be confusing the uses of model.save() and
> form.save() here. Mike Keller needed a save() method in his model for
> the reasons you noted: he wanted to automatically populate some
> particular fields on every save. Regardless of whether the saving is
> done via a form or some other method (e.g. a cronjob or a command-line
> invocation).
> 
> Independent of whether or not you have a custom save() method for your
> models (each model has a default save() method, of course -- it just
> saves the currently populated attributes), you will have form.save() in
> the case you are considering. The form.save() method for a
> from_for_model() result acts as a wrapper that knows how to pull the
> data out of the form, put it into the model and call the model's save()
> method -- whether it be your custom one or the default one.

Your right. I did not realise they were different. Oh!

> The other reason you might not use form.save() is because it is kind of
> specific to form_for_model() forms, so it won't always exist. If I can
> over-simplify things a little, imagine we split the universe of
> Django-created forms into two groups: Group A are those forms which
> correspond exactly to one of your model instances (not just one model,
> but one instance of that model). For example, a "create object" form in
> the admin interface where the input fields correspond precisely to the
> model behind it. Group B are those forms where the input data is then
> munged in some way and split across possibly multiple models for
> storage. An example here might be a form that allows multiple teams for
> a competition to be entered -- the data is split across multiple model
> instances for saving. 
> 
> I find in my own form creation, that I mostly create group B forms, so
> form_for_model() doesn't appear in my code very much. Instead, I am
> responsible for splitting up the data and populating the right models
> and then saving them.
> 
> In short, form.save() is really just a convenience method for one class
> of forms where saving is an easy thing to work out how to do. You may
> not always have it available.

Oh, it's complex. I think I sort of understand the above. Time for me to go 
back a 
bit and create some test cases to try out - next week.

Thanks Malcolm
Have a good weekend to all.
-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help needed in understanding newforms.

2007-03-01 Thread Michael Lake

Malcolm Tredinnick wrote:
> Hi Michael,
> 
>  On Fri, 2007-03-02 at 16:08 +1100, Michael Lake wrote:
>> Im trying to understand newforms and comparing the newforms docs at 
>> http://www.djangoproject.com/documentation/newforms/ with "Using djangos 
>> newforms" at 
>> http://code.pui.ch/2007/01/07/using-djangos-newforms/ and Mike Cantelon's 
>> code at 
>> http://www.mikecantelon.com/?q=node/22

> Forms are view-level functions: a model does not need to be involved
> with forms, or accessible via the web, so you can usually arrange for
> things in models.py to be completely divorced from any knowledge of a
> "request" object and the like.
> 
> Forms, on the other hand, are your interface for user-input -- very much
> view functions. So put form handling code in views.py (or modules used
> by views.py).

That clicks. Good one understanding issue solved :-)

>>Philipp Keller's Example
>>
>>But then he defines this method:
>>def add_entry(request):
>> EntryForm = forms.models.form_for_model(Entry)
>> etc...
>>
>>and I'm not sure where this belongs ... in models.py or views.py.
> 
> 
> It's using "request" -- so it's a view.

I hadn't realised that if it takes a request then it should be in view.
So the request is coming from the user via the web server and it is handled by 
the 
parser that looks at url.py which passes it onto the appropriate view that then 
takes 
the request and acts upon it.
I'll now go through that article again with keeping in mind that it's dealing 
with a 
form so it should be in views.py.

>>What I have got so far
>>I have a small test form working using form_for_model and I have placed the 
>>class 
>>statement in models.py


> This looks reasonable. Are you having problems with it misbehaving, or
> just posting it in case there is some egregious mistake that jumps out?
The latter :-)

>>Also I'm confused on the save methods. Some exemples use form.save() and 
>>others 
>>define a save method under the model Class.
> 
> They serve slightly different purposes. A model class may have specific
> things that need to be done upon saving. 

Hence in Mike Kellers article he defines a save in the Class definition in 
models.py 
as he has self.added and self.changed as datetime things which need to be acted 
upon. 
A default form.save() would not do that I gather so he overrides it?

> Now, again, because forms and
> web-interfaces in general are not the only way to create and update
> model instances, some of the saving logic definitely belongs to the
> model class.
> 
> On the other side, when you automatically create a form from a model
> (using form_for_model(), it creates a special "save" method that is
> really a proxy for the model's save() method, but also checks that there
> are no validation errors in the form and updates the model with the
> forms inputs.
> 
> So form.save() for a form generated from form_for_model() is just a
> quick way to update and save from a form to a model. Does that clear
> things up at all, or just muddy the waters even further?

Clears thins up a bit. So if one can do with the save() from form_for_model 
then one 
is lucky - use it. Otherwise if you have something special like setting a 
datatime 
you have to use your own save().

> Newforms is not something that is immediately obvious, but once it does
> click, I think it seems much easier than the old forms way. I, for one,
> am pretty impressed with Adrian's design work here. It's worth
> persisting with trying to get them to work.

I have started a major project with Django after looking at a few alternatives 
like 
TurboGears or just writing my own cgi stuff. I'll certainly be persisting.
I'll be back next week with more questions :-)

Thanks
Mike

-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to modify form error messages.

2007-03-01 Thread Michael Lake

Malcolm Tredinnick wrote:
> On Fri, 2007-03-02 at 15:27 +1100, Michael Lake wrote:
> 
>>Hi all
>>
>>I see in the documentation for forms that the HTML output will include the 
>>validation 
>>errors as a  near the field. How does one change that? I would like the 
>>errors to 
>>be after the form field similar to this below irrespective of whether im 
>>rendering it 
>>as_p or asdefault table layout.
>>
>>Name: [ ] < This field is required
>>
>>rather than
>>
>>* This field is required
>>Name: [ ]
> 
> 
> Have a look at the errors_on_separate_row parameter in the html_output()
> method of the newsforms.form.Form class. Passing that in as True should
> do what you want. It means you will need to write your own version of
> as_ul() or as_table() if you want to use those methods, but they're only
> convenience methods in any case -- overriding or using your own wrapper
> is encouraged for anything funky.
> 
> So either call html_output() and pass in your format strings manually,
> with a trailing True parameter, or write your own wrapper for this in
> your Form subclass.

Thanks, that gives me a starting point for lots more investigation. I can see 
in 
forms.py on the Django site where as_table() and _html_output() are defined.
It will take me a day to digest it. Searching for 'errors_on_separate_row' also 
gives 
me a few links to follow.

Thanks
-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Help needed in understanding newforms.

2007-03-01 Thread Michael Lake

Hi all

Im trying to understand newforms and comparing the newforms docs at 
http://www.djangoproject.com/documentation/newforms/ with "Using djangos 
newforms" at 
  http://code.pui.ch/2007/01/07/using-djangos-newforms/ and Mike Cantelon's 
code at 
http://www.mikecantelon.com/?q=node/22

Django's Newforms Docs
--
In the newforms docs you subclass Form with "class ContactForm(forms.Form):" to 
create the form object.
1. Should this be in models.py or views.py ?
2. Then one instantiates the class f = ContactForm() but is this in view.py or 
models.py?

Philipp Keller's Example

In the "Using djangos newforms" Philipp Keller uses in models.py
 class Entry(models.Model):
etc...

But then he defines this method:
def add_entry(request):
 EntryForm = forms.models.form_for_model(Entry)
 etc...

and I'm not sure where this belongs ... in models.py or views.py.

Mike Cantelon's Example
---
This is more clear about which file should contain what code.
"ContactForm = forms.form_for_model(Contact)"
is in views.py

Is form_for_model always called from a def within views.py?

Both Philipp Keller and Mike Cantelons code uses form_from_model so its hard to 
follow and compare with the Django newforms docs. (Thanks though to Mike and 
Philipp 
  for placing their code up).

What I have got so far
--

I have a small test form working using form_for_model and I have placed the 
class 
statement in models.py

# In models.py:

class Data(models.Model):
 experiment_id = models.CharField(maxlength=10)
 procedure_id  = models.CharField(maxlength=10)
 field_name= models.CharField(maxlength=255, core=True)
 value = models.CharField(maxlength=255, core=True)

# In views.py

def test(request):

 # form_for_model() returns a Form class, not a Form instance. You have to
 # instantiate the form class before sending it to the template, like so:
 #   SomeForm = forms.form_for_model(Project)
 #   form = SomeForm()
 #   return render_to_response('app/my.html', {'form': form})

 DataForm = forms.form_for_model(Data)

 if request.method == 'POST':
 form = DataForm(request.POST)
 if form.is_valid():
 form.save()
 return HttpResponseRedirect("/")
 else:
form = DataForm()

 data = {'Procedure': 'test',
 'Field': 'temperature',
 'Value': '20C',
 'form': form,
}

 return render_to_response('lab/test.html', data)

# In test.html


   
 {{ form }}
   
   


Also I'm confused on the save methods. Some exemples use form.save() and others 
define a save method under the model Class.

Sorry this is a bit of a long post with many questions. The whole newforms just 
has 
not yet "clicked" :-)

-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How to modify form error messages.

2007-03-01 Thread Michael Lake

Hi all

I see in the documentation for forms that the HTML output will include the 
validation 
errors as a  near the field. How does one change that? I would like the 
errors to 
be after the form field similar to this below irrespective of whether im 
rendering it 
as_p or asdefault table layout.

Name: [ ] < This field is required

rather than

* This field is required
Name: [ ]


Mike
-- 




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to do dynamic lookup of variables.

2007-02-15 Thread Michael Lake

Hi

Benedict Verheyen wrote:
 > When i need such functionality, i assign values to vars of the list items.
 > For instance, in your case i would do something like this
 > def experiments(request):
 > experiment_list = Experiment.objects.all()
 > ecount = len(experiment_list) + 1
 >
 > for e in experiment_list:
 > e.proc_count = e.experiment_procedure_set.all().count()
 > return render_to_response('lab/experiments.html',
 >{'experiment_list': experiment_list,
 >'ecount': ecount,
 >})
 >
 > As you've seen, i reuse the experiment_list to loop over and assign
 > values too.
 > This is from the top of my head so i hope it works :)

Yes it does work, thanks heaps. I presume though that the render_to_return is 
outside 
of the previous for loop as below.

for e in experiment_list:
 e.proc_count = e.experiment_procedure_set.all().count()

return render_to_response('lab/experiments.html',
 {'experiment_list': experiment_list,
  'ecount': ecount,
 })

Anyhow with the above I now get the correct list of procs for each experiment. 
So 
what's happening I understand is that I'm creating a new attribute or method of 
the 
object e called .proc_count by simply assigning e.proc_count a value?


> Mike Lake schreef:
>>Im trying to place into a list of experiments the number of procedures for 
>>each experiment.
>>From within the views.py I can save the number of procedures in an experiment 
>>into either a list of tuples (e.id, count) or a dictionary {'e.id': count}. 
>>But when I come to access them in the templates I can't do something like {{ 
>>e."e.id".1 }}
>>
>>The template:
>>
>>This works: {{ pcount.1.1 }} is the number of procedures in experiment 1.
>>This works: {{ pcount.2.1 }} is the number of procedures in experiment 2.
>>
>>{% for e in experiment_list  %}
>>{{ e.id }}
>>{{ e.name }}
>>{{ e."e.id".1 }}  <- what to use here though ???
>>{% endfor %}
>>
>>In views.py:
>>
>>def experiments(request):
>>experiment_list = Experiment.objects.all()
>>ecount = len(experiment_list) + 1
>>
>># TODO create a dictionary of the number of procedures in each experiment.
>>pcount = []
>>my_tuple = ()
>>for e in Experiment.objects.all():
>>my_count = e.experiment_procedure_set.all().count()
>>pcount.append( (e.id, my_count) )
>>
>>return render_to_response('lab/experiments.html',
>>{'experiment_list': experiment_list,
>> 'ecount': ecount, # ecount is the number of experiments.
>> 'pcount': pcount, # pcount is the number of procedures in an 
>> experiment. 
>>    })
>>
>>There are references to 'dynamic lookup' of variables in the docs for 
>>template authors.
>>But this seems very complex to use custom tags and filters. I know the docs 
>>can be a little behind the development version so is there an easy way to do 
>>this?

Mike

-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Is there a way for a template file to know its name?

2007-02-12 Thread Michael Lake

Hi

Malcolm Tredinnick wrote:
 > There isn't any way to do this built into Django. It may not always make
 > sense, either, since a template could just be a string (hence have no
 > name).
 >
 > Looking through some code I have written, I found a case where I was
 > doing something like you are and I am just passing in a "no_display"
 > variable in the context on the pages that shouldn't have a link like
 > this. I test to see if no_display is false before trying to display the
 > link (in fact, in my case, it's on a help page, too).

Thats perfect, thanks.
I now have
{% if not no_display %}
... a help link in a div 
{% else %}
... a blank div element ...
{% endif %}

and in my views for the just for the help function I have:
render-to_reponse('lab/help.html', {'no_display': 1})

Works a treat. Thanks.

> On Tue, 2007-02-13 at 17:29 +1100, Michael Lake wrote:
>>Is there a way for a template file to know its name? The reason is that I 
>>have a 
>>template file base.html which is placed at the beginning of each HTML file 
>>and it 
>>contains a link to the help page. This will appear on each page and works 
>>fine.
>>
>>However I don't want the help link to appear on the help page itself so I was 
>>wanting 
>>to have something like this in base.html
>>
>>{% if template_name == help.html %}
>>  
>>{% else %}
>>  Help
>>{% endif %}
>>
>>where template_name or something is the name of the current template file 
>>being rendered.

Mike

-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Is there a way for a template file to know its name?

2007-02-12 Thread Michael Lake

Hi all

Is there a way for a template file to know its name? The reason is that I have 
a 
template file base.html which is placed at the beginning of each HTML file and 
it 
contains a link to the help page. This will appear on each page and works fine.

However I don't want the help link to appear on the help page itself so I was 
wanting 
to have something like this in base.html

{% if template_name == help.html %}

{% else %}
Help
{% endif %}

where template_name or something is the name of the current template file being 
rendered.

Mike
-- 

Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help needed with select_related and a Many-to-many relationship.

2007-02-11 Thread Michael Lake

Malcolm Tredinnick wrote:
> On Mon, 2007-02-12 at 13:04 +1100, Michael Lake wrote:
> 
>>Hi all
>>
>>I made this post earlier but since then I have gone back to try and get this 
>>working 
>>by understanding things via the django shell. The app is for a lab where 
>>users will 
>>have a list of experiments. Each experiment consists of several procedures in 
>>order.
>>I want to have a list of experiments and for each experiment all the details 
>>like
>>the procedures for that exp.
>>
>>Very much like the Pizzas and Toppings in the documentation but procedures 
>>are 
>>ordered - as toppings should be for pizzas :-)
>>
>>Im having problems getting a list of procedures for an experiment. In the 
>>shell I can 
>>do this:
>> >>> ... import Experiment, procedure, Experiment_Procedure
>> >>> e=Experiment.objects.select_related().get(id=1)
>> >>> e
>> >>> 
>> >>> e.procedures
>>(gives me that experiments has no attribute procedures)
> 
> 
> Looking at the models you posted, this should be
> e.experiment_procedure_set.all(). You cannot transparently leap through
> the Experiment_Procedure reverse-relation and end up in Procedure. If
> you wanted the related Procedure(s), you would want to do something like
> 
> [ep.procedure for ep in e.experiment_procedure_set.all()]

Ah. I would not have realised that _set.all() was what I wanted. I can see it 
under 
"Related objects" in the Database API docs.
If I do this:
 >>> e.experiment_procedure_set.all()
I get:
 >>> [Experiment_Procedure: <3 56A Reduction>,
  Experiment_Procedure: <1 56A Aliquot division>,
  Experiment_Procedure: <2 56A Test>,]

Excellent!

And also thanks for the short python way to express that loop.
 > [ep.procedure for ep in e.experiment_procedure_set.all()]

Next I'll work on trying to understand the _set.all() and getting this into a 
template.

Mike

-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Help needed with select_related and a Many-to-many relationship.

2007-02-11 Thread Michael Lake

Hi all

I made this post earlier but since then I have gone back to try and get this 
working 
by understanding things via the django shell. The app is for a lab where users 
will 
have a list of experiments. Each experiment consists of several procedures in 
order.
I want to have a list of experiments and for each experiment all the details 
like
the procedures for that exp.

Very much like the Pizzas and Toppings in the documentation but procedures are 
ordered - as toppings should be for pizzas :-)

Im having problems getting a list of procedures for an experiment. In the shell 
I can 
do this:
 >>> ... import Experiment, procedure, Experiment_Procedure
 >>> e=Experiment.objects.select_related().get(id=1)
 >>> e
 >>> 
 >>> e.procedures
(gives me that experiments has no attribute procedures)

I have tried all sorts of things but clearly am confused.
Experiment 56A will have an id in the experiments table (id=1). And it will 
have 
several entries in the experiments_procedures table that links experiments and 
procedures.  I want to follow the foreign keys so I get the procedure_ids 
10,15,5 for 
exp_id=1

The experiments_procedures table might be like this:
id  exp_id  proc_id  sequence
1   1   10   1
2   1   15   2
3   19   3


The details are below.

This is my model (unimportant stuff is cut out):

class Procedure(models.Model):
  name= models.CharField(maxlength=255, core=True)
  description = models.CharField(maxlength=255, core=True)

  def __str__(self):
  return self.name

class Experiment(models.Model):
  name= models.CharField(maxlength=255)
  description = models.CharField(maxlength=255)

  def __str__(self):
  return "%s: %s" % (self.name, self.description)

# I'm using an intermediary table experiment_procedure to implement the 
many-to-many
# with an additional field, sequence, to implement the ordering of the 
procedures.
class Experiment_Procedure:
  experiment = models.ForeignKey(Experiment, core=True)
  procedure =  models.ForeignKey(Procedure)
  sequence = models.PositiveSmallIntegerField()

  def __str__(self):
  return "%s %s %s" % (self.sequence, self.experiment, self.procedure)


Here is an example of the table lab_experiment_procedure and what data it might 
contain:
+---
id  exp_id  proc_id  sequence
1   1   10   1
2   1   15   2
3   19   3
4   2   10   1
5   29   2
+-

As you can see it relates procedures to experiments i.e. what procedures and the
order of those that are in each experiment. The id field would not really be 
used at
all - django just adds it.


-- 
Michael Lake




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



General help and help with an ordered ManyToMany

2007-02-07 Thread Michael Lake

Hi all

Im trying to write my first djano app. It's for a lab where users will have a 
list of 
experiments each experiment consists of several procedures in order. Very much 
like 
the Pizzas and Toppings in the documentation but procedures are ordered - as 
toppings 
should be for pizzas :-)

I want to have a list of experimnents and for each experiment all the details 
like 
the procedures for that exp.

My problem is that I'm a bit confused as to what objects I should be passing as 
context to the templates and in the templates how I should be referencing what 
I pass 
in. Im passing both a list from Experiment.objects.all() and 
Procedures.objects.all() 
into experiments.html but really I should be able to get the procedures list 
from a 
query of the experiment object. The tutes and wiki are good but I still haven't 
get 
got over the first hill of conceptually what I'm doing.

In urls.py I have:

from django.conf.urls.defaults import *
from proteomics_dev.lab.models import Experiment, Procedure

info_dict = {
 'queryset': Experiment.objects.all(),
}

urlpatterns = patterns('',
 # This will invoke methods defined in views.py and gives a list of 
experiments.
 (r'^exps/', 'proteomics_dev.lab.views.experiments'),
 # This is for a generic view which shows the detail for a specific 
experiment.
 (r'^detail/(?P\d+)', list_detail.object_detail, info_dict),


MODELS

# An Experiment consists of a number of procedures in a specified order.
# There will be many procedures in an experiment and a procedure can be in 
several
# experiments.

class Procedure(models.Model):
 name= models.CharField(maxlength=255, core=True)
 description = models.CharField(maxlength=255, core=True)

 def __str__(self):
 return self.name

class Experiment(models.Model):
 name= models.CharField(maxlength=255)
 description = models.CharField(maxlength=255)

 def __str__(self):
 return "%s: %s" % (self.name, self.description)

# I'm using an intermediary table experiment_procedure to implement the 
many-to-many
# with an additional field, sequence, to implement the ordering of the 
procedures.
class Experiment_Procedure:
 experiment = models.ForeignKey(Experiment, core=True)
 procedure =  models.ForeignKey(Procedure)
 sequence = models.PositiveSmallIntegerField()

 def __str__(self):
 return "%s %s %s" % (self.sequence, self.experiment, self.procedure)


Here is an example of the table lab_experiment_procedure and what data it might 
contain:
+---
id  exp_id  proc_id  sequence
1   1   10   1
2   1   15   2
3   19   3
4   2   10   1
5   29   2
+-

As you can see it relates procedures to experiments i.e. what procedures and 
the 
order of those that are in each experiment. The id field would not really be 
used at 
all - django just adds it.


VIEWS

def experiments(request):
 experiment_list = Experiment.objects.all()
 procedure_list = Procedure.objects.all()

 return render_to_response('lab/experiments.html',
{'experiment_list': experiment_list,
 'procedure_list':  procedure_list,
})

def procedures(request):
 procedure_list  = Procedure.objects.all()
 return render_to_response('lab/procedures.html',
{'procedure_list': procedure_list})

TEMPLATES

templates/lab/experiments.html
--

List of Lab Experiments
{% if experiment_list %}
 
 IDNameDescription
 {% for exp in experiment_list  %}
 
 {{ exp.id}}
 {{ exp.name }}
 {{ exp.description }}
 
 
 {% endfor %}
 
{% else %}
 Sorry, no experiments available.
{% endif %}


Testing this is a list of dictionary entries.
 0 {{ filtered_experiment_list.0 }}
 1 {{ filtered_experiment_list.1 }}

Testing {% for proc in procedure_list  %}
   {{ proc.name }}
{% endfor %}


templates/lab/experiment_detail.html


Experiment Detail

ID:  {{ object.id }}
Name:{{ object.name }}
Description: {{ object.description}}


List of procedures for this experiment.
{{ ??? procedure_list.name }}

INCLUDE HERE A LIST OF THE PROCEDURES NAMES AND DESCRIPTIONS FOR THIS EXP.



-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238






--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---