Help with implementing dynamic views/models
This may seem like a very simple question and I have just missed the answer in the piles of documentation and tutorials that I've read over the past two days. I'm new to Django and trying to implement an internal site at work, and I'm the only Python/Django person we have, so this is all on me. What I am doing is this: I have a set of .html files, templates, which are named testn.html (i.e. test1.html, test2.html, etc) Each template extends base.html, but they each have at least 2 divs that I need to populate with HTML that is entered in the admin interface and stored in the Page model. What I need to do is this: from the url parse what test is being requested: url(r'^test(\d{1})/$', test), cal the test view: def test(request, testn): try: testn = str(testn) return direct_to_template(request, template="test%s.html" % testn) except ValueError: raise Http404() And then return the template, but with the correct object attached to it, filtered by name. I can't find a way to do this, all that I can find are ways that make me grab all the objects (and where do I do this? In models.py or views.py? There are conflicting thoughts on this). I really just need to grab the one object, and if it has the fields I need, to populate the template with them. Is there an easy way to do this that won't require me to loop over all objects? Thank you so much for any help or insight! --Laura C. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Help with implementing dynamic views/models
That is exactly what I want to do, I can't seem to understand exactly how to implement that and have it populate in the template. Do I just define the variable in the views and then in the template use {{ variable_name }} where I need it? Because I tried that first and it didn't work. So, maybe I'm just not sure what it is I'm doing exactly ;) Thanks for any help! On Oct 10, 1:09 pm, Brett Epps wrote: > I may be misunderstanding your question, but it sounds like you need to > use Page.objects.get or Page.objects.filter (in your view function) to > look up the particular objects that you want to send to the template. > > Brett > > On 10/10/11 9:53 AM, "xenses" wrote: > > > > > > > > >This may seem like a very simple question and I have just missed the > >answer in the piles of documentation and tutorials that I've read over > >the past two days. I'm new to Django and trying to implement an > >internal site at work, and I'm the only Python/Django person we have, > >so this is all on me. > > >What I am doing is this: I have a set of .html files, templates, which > >are named testn.html (i.e. test1.html, test2.html, etc) Each template > >extends base.html, but they each have at least 2 divs that I need to > >populate with HTML that is entered in the admin interface and stored > >in the Page model. What I need to do is this: > > >from the url parse what test is being requested: > > >url(r'^test(\d{1})/$', test), > > >cal the test view: > > >def test(request, testn): > > try: > > testn = str(testn) > > return direct_to_template(request, template="test%s.html" % > >testn) > > except ValueError: > > raise Http404() > > >And then return the template, but with the correct object attached to > >it, filtered by name. I can't find a way to do this, all that I can > >find are ways that make me grab all the objects (and where do I do > >this? In models.py or views.py? There are conflicting thoughts on > >this). I really just need to grab the one object, and if it has the > >fields I need, to populate the template with them. Is there an easy > >way to do this that won't require me to loop over all objects? > > >Thank you so much for any help or insight! > >--Laura C. > > >-- > >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 > >django-users+unsubscr...@googlegroups.com. > >For more options, visit this group at > >http://groups.google.com/group/django-users?hl=en. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Help with implementing dynamic views/models
I thank you for your help and apologize for my naivete, however I still am not seeing that tag populate in the template. Here is my view function in its entirety: def test(request, testn): try: testn = str(testn) page = Page.objects.filter(name = "test%s" % testn) return direct_to_template(request, template="test%s.html" % testn, extra_context={page:page}) except ValueError: raise Http404() I am using {page:page} because anything else returns errors in debug mode. in my template I have : {{ page.leaderboard }} the page object looks like this: class Page(models.Model): name = models.CharField(max_length=25, verbose_name='Page Name') leaderboard = models.TextField(max_length=500, null=True, blank=True, verbo\ se_name='Leaderboard Tag') rectangle = models.TextField(max_length=500, null=True, blank=True, verbose\ _name='300x250 Tag') rectangle2 = models.TextField(max_length=500, null=True, blank=True, verbos\ e_name='Additional 300x250') def __unicode__(self): return self.name I know that once I figure this out, I'm going to feel rather daft and I appreciate all the help you've given me! Thanks so much! On Oct 10, 2:56 pm, Brett Epps wrote: > The direct_to_template() function can take an extra_context keyword > argument (a dict). So: > > direct_to_template(request, template='blah.html', extra_context={'foo': > bar}) > > Would let you use {{ foo }} in a template to output the value of the > variable bar. > > By the way, as a replacement for direct_to_template, there's > django.shortcuts.render [1], which is a little more concise. (Usually, > you use direct_to_template in urls.py, since it is a full-fledged generic > view function.) > > 1.https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.s... > ts.render > > Hope that helps, > > Brett > > On 10/10/11 12:21 PM, "xenses" wrote: > > > > > > > > >That is exactly what I want to do, I can't seem to understand exactly > >how to implement that and have it populate in the template. Do I just > >define the variable in the views and then in the template use > >{{ variable_name }} where I need it? Because I tried that first and it > >didn't work. So, maybe I'm just not sure what it is I'm doing > >exactly ;) > > >Thanks for any help! > > >On Oct 10, 1:09 pm, Brett Epps wrote: > >> I may be misunderstanding your question, but it sounds like you need to > >> use Page.objects.get or Page.objects.filter (in your view function) to > >> look up the particular objects that you want to send to the template. > > >> Brett > > >> On 10/10/11 9:53 AM, "xenses" wrote: > > >> >This may seem like a very simple question and I have just missed the > >> >answer in the piles of documentation and tutorials that I've read over > >> >the past two days. I'm new to Django and trying to implement an > >> >internal site at work, and I'm the only Python/Django person we have, > >> >so this is all on me. > > >> >What I am doing is this: I have a set of .html files, templates, which > >> >are named testn.html (i.e. test1.html, test2.html, etc) Each template > >> >extends base.html, but they each have at least 2 divs that I need to > >> >populate with HTML that is entered in the admin interface and stored > >> >in the Page model. What I need to do is this: > > >> >from the url parse what test is being requested: > > >> >url(r'^test(\d{1})/$', test), > > >> >cal the test view: > > >> >def test(request, testn): > >> > try: > >> > testn = str(testn) > >> > return direct_to_template(request, template="test%s.html" % > >> >testn) > >> > except ValueError: > >> > raise Http404() > > >> >And then return the template, but with the correct object attached to > >> >it, filtered by name. I can't find a way to do this, all that I can > >> >find are ways that make me grab all the objects (and where do I do > >> >this? In models.py or views.py? There are conflicting thoughts on > >> >this). I really just need to grab the one object, and if it has the > >> >fields I need, to populate the template with them. Is there an easy > >> >way to do this that won't require me to loop over all objects? > > >> >Thank you so much for any help or insight! > >> >--Laura C.