Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
the talent deficit
Dec 20, 2003

self-deprecation is a very british trait, and problems can arise when the british attempt to do so with a foreign culture





bitprophet posted:

Depends on what exactly you want to do; with the newer admin (the one in 1.0, previously known as newforms-admin) you can create multiple different admin sites with different functionality. Can you give a specific example of what you want to accomplish?

The multiple admin sites is exactly what I needed! I'd overlooked that innocuous little section at the bottom of the admin docs. Thanks!

Adbot
ADBOT LOVES YOU

bitprophet
Jul 22, 2004
Taco Defender

the talent deficit posted:

The multiple admin sites is exactly what I needed! I'd overlooked that innocuous little section at the bottom of the admin docs. Thanks!

Yea, it's A) a relatively new feature, B) not used all that often regardless (the OTHER stuff in newforms-admin is generally more interesting) and C) a pretty simple "feature" whose use is largely up to you. All of that combined leads to it getting that tiny little mention.

I kinda wish I'd been able to work an example of that into my book, but we had to release it sooner rather than later :(

Anyway, good luck using it -- let us know if you run into problems or find neat tricks to do with it. I personally haven't actually had to use >1 admin site yet :)

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



I'm trying to add content from the blog app in my project to templates rendered by the main project and I don't think I'm doing it the best way. Right now, I have a view on the main app calling a view on the blog that renders a post using a template and returns the result wich I dump into the main page context. My concern is that the blog post is being rendered whether or not it is being displayed, but I don't know of a better way to do this and keep the template under the control of the blog app which (as I see it) is where it belongs.

king_kilr
May 25, 2007

Munkeymon posted:

I'm trying to add content from the blog app in my project to templates rendered by the main project and I don't think I'm doing it the best way. Right now, I have a view on the main app calling a view on the blog that renders a post using a template and returns the result wich I dump into the main page context. My concern is that the blog post is being rendered whether or not it is being displayed, but I don't know of a better way to do this and keep the template under the control of the blog app which (as I see it) is where it belongs.

I would do an inclusion tag(look for that in the doc), or do something like this: http://www.b-list.org/weblog/2006/jun/07/django-tips-write-better-template-tags/

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.




OK, that was actually easier than I was thinking it was going to be. I'm guessing the propper place to store the template to use is in the model? I don't want the main application to have to know anything about it.

bmoyles
Feb 15, 2002

United Neckbeard Foundation of America
Fun Shoe
from the django.contrib.auth documentation:

quote:

Permissions are set globally per type of object, not per specific object instance. For example, it's possible to say "Mary may change news stories," but it's not currently possible to say "Mary may change news stories, but only the ones she created herself" or "Mary may only change news stories that have a certain status, publication date or ID." The latter functionality is something Django developers are currently discussing.
Given that, what's the current best practice for actually DOING that? Has some 3rd party come up with an authentication/permission scheme such that I can grant people the ability to perform actions on specific objects, rather than specific types?

the talent deficit
Dec 20, 2003

self-deprecation is a very british trait, and problems can arise when the british attempt to do so with a foreign culture





bmoyles posted:

from the django.contrib.auth documentation:

Given that, what's the current best practice for actually DOING that? Has some 3rd party come up with an authentication/permission scheme such that I can grant people the ability to perform actions on specific objects, rather than specific types?

There's a branch in the repository with row level permissions, but I have no idea how stable/useable it is.

I've been playing with subclassing admin sites the last day and a half, and you could get row level permissions that way, if you're willing to write some views.

ijustam
Jun 20, 2005

Has anyone worked with matplotlib before? There are very few examples or documentation regarding Django integration :( I'm not really sure where to begin.

Or maybe someone can offer an alternative graphing library?

ijustam fucked around with this message at 02:16 on Oct 11, 2008

bitprophet
Jul 22, 2004
Taco Defender

ijustam posted:

Has anyone worked with matplotlib before? There are very few examples or documentation regarding Django integration :( I'm not really sure where to begin.

Or maybe someone can offer an alternative graphing library?

Well, the trick with Django is there is no "integration": Django is just plain old Python that happens to be modeling how the Web works. Using a given Python library is, in the base case, as simple as 'import matplotlib' wherever you're trying to use it.

What exactly are you trying to accomplish with matplotlib and Django? Serve up dynamically generated graph images?

ijustam
Jun 20, 2005

bitprophet posted:

Well, the trick with Django is there is no "integration": Django is just plain old Python that happens to be modeling how the Web works. Using a given Python library is, in the base case, as simple as 'import matplotlib' wherever you're trying to use it.

What exactly are you trying to accomplish with matplotlib and Django? Serve up dynamically generated graph images?

Pretty much, yeah.

I guess what I'm struggling with is passing the output image as an HttpResponse. The one example I found does this:

code:
    response=django.http.HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response
:psyduck:

the talent deficit
Dec 20, 2003

self-deprecation is a very british trait, and problems can arise when the british attempt to do so with a foreign culture





Not ideal, but is there a reason you can't just save the image to the filesystem, then pass the url to the request context? (Or redirect to it)? Just setup a cron to periodically remove images.

duck monster
Dec 15, 2004

the talent deficit posted:

Not ideal, but is there a reason you can't just save the image to the filesystem, then pass the url to the request context? (Or redirect to it)? Just setup a cron to periodically remove images.

This is probably the best way, since you could then cache the image to save CPU

edit: Variation on the pattern. You point apache to the image directory where it shits out the image. Set it up so that if it 404s , it will fall back to the code that generates the image, which would then serve the image up for it (and supress the 404 code from being sent to the browser, because thats bad mojo) and then in future django wouldnt even need to be hit for it.

disclaimer: I know how to do it and make it work with mod_python by writing a custom handler, but I have no idea how to make it work for django.

duck monster fucked around with this message at 06:40 on Oct 11, 2008

bitprophet
Jul 22, 2004
Taco Defender

ijustam posted:

I guess what I'm struggling with is passing the output image as an HttpResponse. The one example I found does this:

code:
    response=django.http.HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response
:psyduck:

No need for psyduck -- that's actually how it's done! The deal is that HttpResponse is modeling exactly that: an HTTP response. HTTP responses are not always text -- every time vanilla Apache serves up a static image or whatnot, it's doing roughly the same thing that code does (stuffing binary image data into the HTTP response).

The main trick is that when you create your new Response, you specify its Content-Type HTTP header so the browser knows what to do with it. The other trick is that HttpResponse objects are "file-like" and can be written to by any Python code that expects to deal with a file object...like, say, a drawing library's canvas.print_png(file_obj) method ;) Again, this will work regardless of the actual data being written -- text or image or PDF or whatever.

the talent deficit posted:

Not ideal, but is there a reason you can't just save the image to the filesystem, then pass the url to the request context? (Or redirect to it)? Just setup a cron to periodically remove images.

This is another, probably better way, because -- as duck monster pointed out -- having Django dynamically generate the graph every request is going to have far worse performance than talent deficit's method, which lets you basically "cache" the result of your drawing/graphing code on the filesystem.

Therefore, depending on exactly what your situation is, you could only generate a new image once in a while and have that result statically served up outside of Django the rest of the time. For example, if you're graphing network traffic or some poo poo, it'd be fine to have the graph update itself every minute or every 5 minutes or whatever.

duck monster posted:

Set it up so that if it 404s , it will fall back to the code that generates the image, which would then serve the image up for it (and supress the 404 code from being sent to the browser, because thats bad mojo) and then in future django wouldnt even need to be hit for it.

This isn't really possible with Django, insofar as you have to load up Django at least a little bit before Django can know whether it's needed! But, as above, you can have Django skip the image creation code unless a new image needs to be generated, which will still be a big time saver. (Yes, you could write a new mod_python handler or modify Django's default one, but I'm guessing that's a bit over ijustam's head if he's still not 100% comfortable with HttpRequests :))

nbv4
Aug 21, 2002

by Duchess Gummybuns
I went through the instructions in the OP and everything worked with version 0.96

But I now upgraded to 1.0, and there is one thing that does not work. When I go to the admin page, non of my custom models are showing up ("categories" in the OP's example). I'm thinking the problem is either with the way I added 'main' to setings.py, or the way I defined "Class Admin" in models.py?

one of my models looks like this:

code:
class Airport(models.Model):

	identifier = models.CharField(max_length=8)
	lattitude = models.DecimalField(max_digits=10, decimal_places=7)
	longitude = models.DecimalField(max_digits=10, decimal_places=7)
	
	name = models.CharField(max_length=64)
	city = models.CharField(max_length=64)
	state = USStateField()
	country = models.CharField(max_length=32)
	
	def __unicode__(self):
		return self.identifier
		
	class Admin:
		pass

	class Meta:
		pass
and in settings.py I have this:

code:
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'main',
)
When I run "python manage.py syncdb", no errors popup and it creates the tables just fine.

hey mom its 420
May 12, 2007

Give this a read: http://docs.djangoproject.com/en/dev/intro/tutorial02/#intro-tutorial02
You don't make models part of the admin interface by putting an inner class of Admin in the models anymore.

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

bitprophet posted:

duck monster posted:

Set it up so that if it 404s , it will fall back to the code that generates the image, which would then serve the image up for it (and supress the 404 code from being sent to the browser, because thats bad mojo) and then in future django wouldnt even need to be hit for it.
This isn't really possible with Django, insofar as you have to load up Django at least a little bit before Django can know whether it's needed! But, as above, you can have Django skip the image creation code unless a new image needs to be generated, which will still be a big time saver. (Yes, you could write a new mod_python handler or modify Django's default one, but I'm guessing that's a bit over ijustam's head if he's still not 100% comfortable with HttpRequests :))

If you have lighty or nginx in front of django (which you should) you can still allow them to serve the graphic by using the header trickery I posted a while back for sending FS files.

duck monster
Dec 15, 2004

http://code.djangoproject.com/wiki/Dia2Django

Its a plug in script for Dia (which is a slut to get running on Os/X, but thats another story) that shits out model files out of UML drawings.

Works a loving charm!

nbv4
Aug 21, 2002

by Duchess Gummybuns
I've been developing a PHP webapp for the past year or so. It's working fine right now, but when I started the project, I didn't really know that much about web development, so it didn't get a very good foundation to start on. A year later, everything works, but the code is a clusterfuck. There are certain things I'd like to change about my app, but since the code is so unorganized, it'll take forever.

I decided my best course of action is to just start over, this time with a framework of some kind to make sure I don't get the project off on the wrong foot. I've decided on Django, but it's hard because every tutorial out there seems to focus on how to use each individual tool of Django, rather that giving you an idea of how the whole system should work project wide. In other words, they all seem to be written as if Django was a language, rather than a framework.

I'm looking for a more "top down" understanding of Django. Something like a tutorial that takes a large project, and breaks all the parts down, instead of a tutorial that starts with nothing, and ends up with a project. Does such a tutorial exist? Does anyone know of a large project that exists where the source is available so that I can look through it?

king_kilr
May 25, 2007

nbv4 posted:

I've been developing a PHP webapp for the past year or so. It's working fine right now, but when I started the project, I didn't really know that much about web development, so it didn't get a very good foundation to start on. A year later, everything works, but the code is a clusterfuck. There are certain things I'd like to change about my app, but since the code is so unorganized, it'll take forever.

I decided my best course of action is to just start over, this time with a framework of some kind to make sure I don't get the project off on the wrong foot. I've decided on Django, but it's hard because every tutorial out there seems to focus on how to use each individual tool of Django, rather that giving you an idea of how the whole system should work project wide. In other words, they all seem to be written as if Django was a language, rather than a framework.

I'm looking for a more "top down" understanding of Django. Something like a tutorial that takes a large project, and breaks all the parts down, instead of a tutorial that starts with nothing, and ends up with a project. Does such a tutorial exist? Does anyone know of a large project that exists where the source is available so that I can look through it?

I'd check out James Bennet's Practical Django projects, the code is NOT fully up to date with 1.0, but most of it will work unchanged, and it does exactly what you want.

duck monster
Dec 15, 2004

pluggables is worth a browse as well.

But I can give you the big picture right here;-

Work out your data structure. Write it using the model specification syntax (Just properties on some descended classes). Its pretty easy.

Get your graphic designer (or do it yourself) to draw a template. I just use dreamweaver and drop the tags into it.

Now write a bunch of views that take data from the models and poo poo them into the templates

shazam. you have a django app!

duck monster fucked around with this message at 09:14 on Oct 13, 2008

hitze
Aug 28, 2007
Give me a dollar. No, the twenty. This is gonna blow your mind...

Would anyone happen to know why I get "500 Server Error" when I turn debugging off in Djangos settings? This has stumped me :(

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

hitze posted:

Would anyone happen to know why I get "500 Server Error" when I turn debugging off in Djangos settings? This has stumped me :(

If you run your project using the command-line interpreter, it should print an exception traceback with information on where the failure is.

The B Man
Mar 21, 2007
'Cause life's too short to play Freebird
I've been searching all over for answers but can't get this to work. Basically the relevant model that I'm dealing with is:

code:
class Song(models.Model):
        Title = models.CharField(max_length=255)
        PerformedBy = models.CharField(max_length=255)
        ACCESS_CHOICES = (
            ('pub', 'Public'),
            ('pri', 'Private'),
            ('pdp', 'Public Domain'),
            ('fri', 'Friends')
        )
        Access      = models.CharField(max_length=3, choices=ACCESS_CHOICES)
        Owner       = models.ForeignKey(User)
        IntroText   = models.CharField(max_length=255,null=True,blank=True)
        Copyright   = models.CharField(max_length=255,null=True,blank=True)
        PublishDate = models.DateTimeField(auto_now_add=True)
        CoAuthors   = models.ManyToManyField(User,related_name="Owner",null=True,blank=True)
        Slug        = models.CharField(max_length=60,unique=True)
        def __unicode__(self):
            return self.Title
        class Meta:
            pass
What I'd like to do is given a user get all the songs that they're either the owner of or the coauthor of. So far I've tried by filtering but can't seem to filter on the coauthor field. I've tried accessing user.song_set but that only gives the ones they own. Any ideas?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



The B Man posted:

I've been searching all over for answers but can't get this to work. Basically the relevant model that I'm dealing with is:

code:
class Song(models.Model):
        Title = models.CharField(max_length=255)
        PerformedBy = models.CharField(max_length=255)
        ACCESS_CHOICES = (
            ('pub', 'Public'),
            ('pri', 'Private'),
            ('pdp', 'Public Domain'),
            ('fri', 'Friends')
        )
        Access      = models.CharField(max_length=3, choices=ACCESS_CHOICES)
        Owner       = models.ForeignKey(User)
        IntroText   = models.CharField(max_length=255,null=True,blank=True)
        Copyright   = models.CharField(max_length=255,null=True,blank=True)
        PublishDate = models.DateTimeField(auto_now_add=True)
        CoAuthors   = models.ManyToManyField(User,related_name="Owner",null=True,blank=True)
        Slug        = models.CharField(max_length=60,unique=True)
        def __unicode__(self):
            return self.Title
        class Meta:
            pass
What I'd like to do is given a user get all the songs that they're either the owner of or the coauthor of. So far I've tried by filtering but can't seem to filter on the coauthor field. I've tried accessing user.song_set but that only gives the ones they own. Any ideas?

I think you want
code:
Song.objects.get(
    Q(author = theUser)|
    Q(coauthors__contains = theUser)
)
At least thats what I get from http://docs.djangoproject.com/en/dev/topics/db/queries/#topics-db-queries

The B Man
Mar 21, 2007
'Cause life's too short to play Freebird

Munkeymon posted:

I think you want
code:
Song.objects.get(
    Q(author = theUser)|
    Q(coauthors__contains = theUser)
)
At least thats what I get from http://docs.djangoproject.com/en/dev/topics/db/queries/#topics-db-queries

That brings me back to a problem I was having earlier, "Related Field has invalid lookup: contains"

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



The B Man posted:

That brings me back to a problem I was having earlier, "Related Field has invalid lookup: contains"

Actually, try it without the __contains, since that's for text searching (as in WHERE user_name LIKE '%jerkboat%').

nbv4
Aug 21, 2002

by Duchess Gummybuns
I'm trying to figure out how to get css files and the like to work in django, but I'm having no luck. I have this in my urls.py:

code:
	(r'^media/(?P<path>.*)$', 'django.views.static.serve',
		{'document_root': '/home/nbv4/dj_logbook/media', 'show_indexes': True}),
the name of my project is "dj_logbook", which is in my home directory, and inside that directory I have a css folder with css files, and a logo folder with a bunch of PNG's. When I direct my browser to "http://127.0.0.1:8000/media/css" I get this error:

code:
Permission denied: /usr/lib/python2.5/site-packages/django/contrib/admin/media/css
Trying to actually use stuff in that folder fails as well, such as <img src="logo/logo1.png">

Does anybody know what the gently caress? I'm going off this guide: http://docs.djangoproject.com/en/dev/howto/static-files/

Is this just another outdated guide?

edit: and in my server window, I get this:

code:
[14/Oct/2008 11:41:32] "GET /home HTTP/1.1" 301 0
[14/Oct/2008 11:41:32] "GET /home/ HTTP/1.1" 200 1555
[14/Oct/2008 11:41:32] "GET /home/css/style_1.css HTTP/1.1" 404 2068
[14/Oct/2008 11:41:32] "GET /home/javascripts/home.js HTTP/1.1" 404 2080
[14/Oct/2008 11:41:32] "GET /home/logo/loggin_1.png HTTP/1.1" 404 2074

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"
Second problem is easy: You need to use absolute paths, like <img src="/path-to-media/logo/logo1.png"/>. In your template context, set "MEDIA_URL = django.conf.settings.MEDIA_URL". Or use a template tag library that does that for you, such as:

* django-template-utils
* django-resourcetags

First is trickier -- I think the development server is hardcoded to direct ^media/ to the admin media directory. Have you tried changing your MEDIA_URL setting to "/site-media/" and then the URLconf entry to (r'^site-media/(...?

nbv4
Aug 21, 2002

by Duchess Gummybuns

Janin posted:

Second problem is easy: You need to use absolute paths, like <img src="/path-to-media/logo/logo1.png"/>. In your template context, set "MEDIA_URL = django.conf.settings.MEDIA_URL". Or use a template tag library that does that for you, such as:

* django-template-utils
* django-resourcetags

First is trickier -- I think the development server is hardcoded to direct ^media/ to the admin media directory. Have you tried changing your MEDIA_URL setting to "/site-media/" and then the URLconf entry to (r'^site-media/(...?

I changed my url conf to site-media and now when I go to 127.0.0.1:8000/site-media, I get a directory listing! But as far as getting the files to actually show up in my html files, the only thing I can get to work is using the absolute full path:

code:
src="http://127.0.0.1:8000/site-media/css/style_1.css"
which is probably not the best way, but eh at least it works. Django seems to have a hardon for absolute paths anyway, so I'm going to have to change a buttload of paths when I "go production" anyway...

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

nbv4 posted:

I changed my url conf to site-media and now when I go to 127.0.0.1:8000/site-media, I get a directory listing! But as far as getting the files to actually show up in my html files, the only thing I can get to work is using the absolute full path:

code:
src="http://127.0.0.1:8000/site-media/css/style_1.css"
which is probably not the best way, but eh at least it works. Django seems to have a hardon for absolute paths anyway, so I'm going to have to change a buttload of paths when I "go production" anyway...

src="/site-media/css/style_1.css"

If you intend to change the media URL between development / production, use one of the libraries I linked.

The B Man
Mar 21, 2007
'Cause life's too short to play Freebird

Munkeymon posted:

Actually, try it without the __contains, since that's for text searching (as in WHERE user_name LIKE '%jerkboat%').

Thanks, that did the trick. I just had to change it to a .filter rather than a .get so I could get all the songs.

nbv4
Aug 21, 2002

by Duchess Gummybuns

Janin posted:

src="/site-media/css/style_1.css"

If you intend to change the media URL between development / production, use one of the libraries I linked.

ah, before I wasn't putting the trailing slash in front of "site-media"...

Now another question. Is there anyway to get ahold of the data in the User.get_profile() method from within the template engine? I have a custom class hooked up to the auth class as described here: http://docs.djangoproject.com/en/dev/topics/auth/?from=olddocs#storing-additional-information-about-users

Is this data somehow added automatically already to the ContextRequest class, or do I have to make my own context processor to add it manually?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



nbv4 posted:

Is there anyway to get ahold of the data in the User.get_profile() method from within the template engine?

Put this in the request context in the view?
code:
{'profile_stuff':request.user.get_profile()}

The B Man posted:

Thanks, that did the trick. I just had to change it to a .filter rather than a .get so I could get all the songs.

Oh, sorry, I was misunderstanding the purpose of the get() method :blush:

nbv4
Aug 21, 2002

by Duchess Gummybuns

Munkeymon posted:

Put this in the request context in the view?
code:
{'profile_stuff':request.user.get_profile()}

But doesn't that kind of defeat the purpose of the RequestContext class? If you have 30 view functions, putting that code in each one can get kind of tedious. Right now I have a context processor that adds a "profile" object to my context by default and it works fint, but I'm not sure if I'm duplicating functionality here.

bitprophet
Jul 22, 2004
Taco Defender

nbv4 posted:

But doesn't that kind of defeat the purpose of the RequestContext class? If you have 30 view functions, putting that code in each one can get kind of tedious. Right now I have a context processor that adds a "profile" object to my context by default and it works fint, but I'm not sure if I'm duplicating functionality here.

First off, if you're just interested in the results of a function and that function doesn't need any parameters, you can just call it in the template. So if you're using RequestContext for the template in question, just {{ request.user.get_profile }} should work -- it'll be directly equivalent to request.user.get_profile().

If you need to do something to/with that info which can't be done within the template language (i.e. if get_profile took arguments or you need to do something to its results each time) then a context processor is the way to go.

nbv4
Aug 21, 2002

by Duchess Gummybuns

bitprophet posted:

First off, if you're just interested in the results of a function and that function doesn't need any parameters, you can just call it in the template. So if you're using RequestContext for the template in question, just {{ request.user.get_profile }} should work -- it'll be directly equivalent to request.user.get_profile().

If you need to do something to/with that info which can't be done within the template language (i.e. if get_profile took arguments or you need to do something to its results each time) then a context processor is the way to go.

get_profile returns an object, so to use it in a template, you must do something like "{{ request.user.get_profile.posts_per_page }}", which unfortunately doesn't work.

Wulfeh
Dec 1, 2005

The mmo worth playing: DAoC

nbv4 posted:

get_profile returns an object, so to use it in a template, you must do something like "{{ request.user.get_profile.posts_per_page }}", which unfortunately doesn't work.

That example you just put works perfectly fine, why do you say it doesn't?

What bitprophet said is correct, if posts_per_page took some args, then you would have to do it the other way.

tcquest
Aug 13, 2003
Go Away!

nbv4 posted:

get_profile returns an object, so to use it in a template, you must do something like "{{ request.user.get_profile.posts_per_page }}", which unfortunately doesn't work.
I think you mean to be using 'user' instead of 'request.user'. so try:
code:
 {{user.get_profile.posts_per_page }} 

bitprophet
Jul 22, 2004
Taco Defender
They're correct, it should work fine. As long as a method can be called with no arguments, you can use it in template expressions and continue chaining on top of it -- the template renderer will turn it into e.g. request.user.get_profile().some_attribute.

Adbot
ADBOT LOVES YOU

nbv4
Aug 21, 2002

by Duchess Gummybuns

bitprophet posted:

They're correct, it should work fine. As long as a method can be called with no arguments, you can use it in template expressions and continue chaining on top of it -- the template renderer will turn it into e.g. request.user.get_profile().some_attribute.

Actually you're right. I was confused. The problem I was having was if the user isn't logged in, get_profile returns nothing. In that case, I'm pretty sure the only solution is to use a context processor, in order to make a profile object regardless if the user is logged in or not.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply