how to do this?

23 views
Skip to first unread message

doniyor

unread,
May 26, 2012, 3:17:58 AM5/26/12
to django...@googlegroups.com
hey guys, i need again a small help. i want to create so many htmls as the number of objects of my model in db. i am trying, but it is stopping after the first creation, because i am doing render_to_response and after the first render_to_response it is not going to the second loop, because i "return",  

how is it possible? 

this is my code.
 
for p in Produkt.objects.all():
            col = p.arg1
            row = p.arg2
            t = Template('<p> test </p>')
            c = Context({'col': col, 'row':row})
            html = t.render(c)
            return render_to_response('book.html', {'html': html}, context_instance=RequestContext(request))
        return render_to_response('book.html', {'titel': titel}, context_instance=RequestContext(request))

thanks many 

doniyor

unread,
May 26, 2012, 3:37:25 AM5/26/12
to django...@googlegroups.com
Sorry i forgot my {{ col}} and {{row}} variables in template, please assume that it is there, i just forgot in posting in forum..

kenneth gonsalves

unread,
May 26, 2012, 3:50:44 AM5/26/12
to django...@googlegroups.com
possibly you need this:
in your view:
products = Product.objects.all()

and in your template:

{% for p in products %}
{{p.arg1}} {{p.arg2}}
{% endfor %}
--
regards
Kenneth Gonsalves

doniyor

unread,
May 26, 2012, 4:08:47 AM5/26/12
to django...@googlegroups.com
Yeah, but then i dont create htmls. What i want is to create so many htmls as the number of objects in db. Is it possible to go around the “return“ till the end of loop? Then i will have all htmls created with rendered values. Is it possible to render a html particle wihthout return statement.. I am also thinking now..

Thanks man ;)

kenneth gonsalves

unread,
May 26, 2012, 4:15:19 AM5/26/12
to django...@googlegroups.com
use ajax?
--
regards
Kenneth Gonsalves

doniyor

unread,
May 26, 2012, 4:26:41 AM5/26/12
to django...@googlegroups.com
Is it then possible to start ajax response from view function without any ajax request from client-site? but i think this wont solve the problem because even in ajax function i need return statement right?

Daniel Roseman

unread,
May 26, 2012, 5:20:57 AM5/26/12
to django...@googlegroups.com
On Saturday, 26 May 2012 09:08:47 UTC+1, doniyor wrote:
Yeah, but then i dont create htmls. What i want is to create so many htmls as the number of objects in db. Is it possible to go around the “return“ till the end of loop? Then i will have all htmls created with rendered values. Is it possible to render a html particle wihthout return statement.. I am also thinking now..

Thanks man ;)

You haven't explained why you want to do this. What are you going to do with all these "htmls"? What's the point of creating them individually? Where will they be used?
--
DR. 

doniyor

unread,
May 26, 2012, 10:03:47 AM5/26/12
to django...@googlegroups.com
the reason why i want to do this is this: 

the user should be able to select products from products table as many as he wants, at each selection i will save the product information in db, then the user clicks on "book" button, which leads to another book.html template where extra fields should be created for data of each selected product. 

for example: user has selected 3 items from products table and then goes to next step where he sees what he has selected and then he submits the booking with some last information of his personal. the  point is that if he has selected 3 items, then there are 3 items in db, so i create 3 html input fields for each selected item where i will render the data that i get from db. thats why i want to create these html input fields according to the number of items in db.. 

Daniel Roseman

unread,
May 26, 2012, 5:31:48 PM5/26/12
to django...@googlegroups.com
On Saturday, 26 May 2012 15:03:47 UTC+1, doniyor wrote:
the reason why i want to do this is this: 

the user should be able to select products from products table as many as he wants, at each selection i will save the product information in db, then the user clicks on "book" button, which leads to another book.html template where extra fields should be created for data of each selected product. 

for example: user has selected 3 items from products table and then goes to next step where he sees what he has selected and then he submits the booking with some last information of his personal. the  point is that if he has selected 3 items, then there are 3 items in db, so i create 3 html input fields for each selected item where i will render the data that i get from db. thats why i want to create these html input fields according to the number of items in db.. 

But that doesn't explain why you want to do it. Say you do as you describe, and you've got x separate "htmls", however that happens. What do you do with them now? They're of no use unless you show them to the user. So you need to somehow concatenate them and include them into a surrounding HTML page. Which is exactly what Kenneth's suggestion of doing it all in the template would achieve.
--
DR.

doniyor

unread,
May 27, 2012, 1:41:14 AM5/27/12
to django...@googlegroups.com
oh yeah. thats true, i thought, that for-loop which Kenneth proposed doesnot create fields but it does, so in every loop there will be a new html field created if i do like this: 
{% for p in products %}
 <p >{{p.arg1}} and {{p.arg2}}</p>
{% endfor %}

thats the easiest way, great! i was thinking totally wrong and different. this is exactly what i want.  

Many thanks to you and Kenneth again 

kenneth gonsalves

unread,
May 27, 2012, 1:40:58 AM5/27/12
to django...@googlegroups.com
On Sat, 2012-05-26 at 07:03 -0700, doniyor wrote:
> for example: user has selected 3 items from products table and then
> goes to
> next step where he sees what he has selected and then he submits the
> booking with some last information of his personal. the point is that
> if
> he has selected 3 items, then there are 3 items in db, so i create 3
> html
> input fields for each selected item where i will render the data that
> i get
> from db. thats why i want to create these html input fields according
> to
> the number of items in db..

this is easily done with a for loop in the template
--
regards
Kenneth Gonsalves

doniyor

unread,
May 27, 2012, 2:07:26 AM5/27/12
to django...@googlegroups.com
yeah, you are right.. now i have another html-related problem. well, now i have even all html fields created thru for-loop. but now all of them have the same attr value: name="prod". and now i want to refer to all of them and send all as email to the buyer. how can i make reference to all field names or is there a way of giving names dynamically... i am stuck cos now in my view i have this line which get the data from field.. 

prod = request.POST.get('prod'). but it is getting only the first value in the list, because others have also the same name 'prod'. i want to get all of them.. 

sorry for being dumb 

doniyor

unread,
May 27, 2012, 2:43:23 AM5/27/12
to django...@googlegroups.com
okay i solved it by rendering a list of names 
Reply all
Reply to author
Forward
0 new messages