You are not logged in.

Change Language:

Freelancer? Consultant? Check out: WorkTrail - Time Tracking made easy
Board » Django » Templates » Template/Model/Form connection

Hi members,

I'm dealing with the access dictionary thing right now. Would you have a look at this example?

Model code
class Project(models.Model):
	title = models.CharField(max_length=255)


So I have a model with a simple property: title.

Now I want to let users to vote for these "projects".
I get all the projects from a database and want to present checkboxes for voting on these projects.
I have a choices -Dictionary with keys for the project ids and 0 or 1 for the choice.

So I put the projects-list and the choices-dict to this template ( I left out the input-checkbox-form-stuff ):

        {% for project in projects %}
        <tr>
            <td>{{project.title}}</td>
            <td align="center">
                ++ {{ choices|get:{{project.id}} }} ++
            </td>
        </tr>
        {% endfor %}


For this I wrote a simple template filter like this:
@register.filter(name='get')
def get(variable, key):
    return variablekey


The problem is: The filter is working in principle, but only when using the hardcoded way:
{{ choices|get:'2' }}


I can't replace the hardcoded '2' with {{project.id}}. This is what i want to do.

What is it I am doing wrong?

I don't want to change the object model, because the votes are only used once...

Hope you can understand my problem...

Kind regards
Peter

--- Zuletzt bearbeitet von petergabriel am 2012-07-23 04:47:46 ---

--- Zuletzt bearbeitet von petergabriel am 2012-07-23 04:49:37 ---

--- Zuletzt bearbeitet von petergabriel am 2012-07-23 04:50:33 ---

--- Zuletzt bearbeitet von petergabriel am 2012-07-23 04:51:52 ---
Ok, this is a bit of a "poor" solution. I created a big dictionary with all values i need...

The real problem is a bit more complex, I didn't want a simple vote, I wanted a "priority election" for the projects...in principle the same problem...

Code:
pw = get_object_or_404(ProjectWeek, pk=pw_id)
    areas = pw.area_set.all()
    
    a = dict()
    for area in areas:
        plist = []
        for project in area.project_set.all():
            p = dict()
            p['id'] = project.id
            p['title'] = project.title
            p['choice'] = 2  # for example 2
            plist.append(p)
        a[area.title] = plist
    
    return render_to_response('projects/projects_election.html', {'areas': a}, context_instance=RequestContext(request))

TEMPLATE:
<form method="POST">
{% csrf_token %}
<table>  
        {% for key,projects in areas.items %}
        <tr><td align="center" colspan=2><br/><h3>{{key}}</h3></td></tr>
        {% for project in projects %}
        <tr>
            <td>{{project.title}}</td>
            <td align="center">
                {% for i in  1234|make_list %}
                <input type="radio" {% if project.choice == forloop.counter %} checked="checked"  {% endif %} name="p{{project.id}}" value="{{i}}"/>{{i}} &nbsp;
                {% endfor %}
                 <input type="radio" {% if project.choice == 0 %} checked="checked"  {% endif %} name="p{{project.id}}" value="0"/> nicht gewählt   
            </td>
        </tr>
        {% endfor %}
        {% endfor %}

</table>
<input type="submit" name="submit" value="Absenden"/>
</form>



A special problem: this won't work:
{% if project.choice == {{i}} %} 

This neither
{% if project.choice == i %} 


I don't know why.. you?

My approach is not nice...if someone knows a better approach, please post.

--- Zuletzt bearbeitet von petergabriel am 2012-07-23 08:34:29 ---

--- Zuletzt bearbeitet von petergabriel am 2012-07-23 08:35:02 ---





Powered by Sphene Community Tools