django-views

How to chain views in Django?

I'm implementing James Bennett's excellent django-contact-form but have hit a snag. My contact page not only contains the form, but also additional flat page information. Without rewriting the existing view the contact form uses, I'd like to be able to wrap, or chain, the views. This way I could inject some additional information vi...

Django Model returning NoneType

I have a model Product it has two fields size & colours among others colours = models.CharField(blank=True, null=True, max_length=500) size = models.CharField(blank=True, null=True, max_length=500) In my view I have current_product = Product.objects.get(slug=title) if len(current_product.size) != 0 : current_product.size = cur...

what is the right way to validate if an object exists in a django view without returning 404?

basically just verify if an object exists and return the object. then based on that perform actions. I'm wondering whats the right way to do it without returning a 404? try: listing = RealEstateListing.objects.get(slug_url = slug) except: listing = None if listing: ...

How to write a Django view to obtain CharFields of multiple Django models that can only be accessed through relations

Please see the following Django models: - class Student(models.Model): reference_num = models.CharField(max_length=50, unique=True) name = models.CharField(max_length=50) birthdate = models.DateField(null=True, blank=True) is_active = models.BooleanField(db_index=True) class Examination(models.Model): short_name = ...

Select Distinct Years and Months for Django Archive Page

I want to make an archive_index page for my django site. However, the date-based generic views really aren't any help. I want the dictionary returned by the view to have all the years and months for which at least one instance of the object type exists. So if my blog started in September 2007, but there were no posts in April 2008, I cou...

How to send a session message to an anonymous user in a Django site?

I often show messages about user actions to logged in users in my Django app views using: request.user.message_set.create("message to user") How could I do the same for anonymous (not logged in) users? There is no request.user for anonymous users, but the Django documentation says that using the "session" middleware you can do the sa...

Auto GET to argument of view

some_view?param1=10&param2=20 def some_view(request, param1, param2): Is such possible in Django? ...

How can I use Django admin list and filterering in my own views?

I’m just beginning to learn Django and I like the automatic listing in Django admin and the way you can configure filters and what columns to show. Is it possible to use it in my own applications? I’ve looked in the source for the admin and figured out that I probably want to subclass the “ChangeList”-object in some way and use it in my...

Getting local dictionary for function scope only in Python

I keep ending up at this situation where I want to use a dictionary very much like the one 'locals' gives back, but that only contains the variables in the limited scope of the function. Is there a way to do this in python? A bit more about why I want to do this: I'm playing with Django and when I go to give my templates context, I am ...

Django - counting model instance views (for a "top entries" app)

I'm new, and confused. I want to create a module that keeps track of the "top hit" instances of both an article and a blog model. I don't want to touch the code for the article or blog models. Is this a candidate for middleware? looking at the HttpRequest.path? Thanks ...

where do functions that don't display go in django

I have some links on an html page like , , currently I handle them as so <p> <a href="/cases/{{case.id}}/case_rate/-">rate down</a> and have a url.py entry: (r'^cases/(?P<case_id>\d+)/case_rate/(?P<oper>.)$', 'mysite.cases.views.case_rate'), then I have a view function that handles the logic and hits the DB, then does this: retu...

changing options according to the user choice

Hi, In my class I have about 12 fields. One field is status, another is reason. When I go to the edit page in the django admin, I only want to show the second field (reason field) if the status=='rejected'. The problem is very simple: just showing the fields according to the user input. ...

Specifying Django Related Model Sort Order

I have Django models that are lists of items and I want the items on each list to have a separate sort order. Maybe list 1 will sort line items by name, list 2 by date, and list 3 by priority. The models looks more or less like this: class ListItem(models.Model): priority = models.IntegerField(choices=PRIORITY_CHOICES) name = ...

Getting all items less than a month old

Is there a way to get all objects with a date less than a month ago in django. Something like: items = Item.objects.filter(less than a month old).order_by(...) ...

Http verb decorator for Django?

In ASP.NET MVC, you can use the AcceptVerbs attribute to correlate a view function with a verb: public ActionResult Create() { // do get stuff } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(FormCollection collection) { // do post stuff } The Django Book suggests something like this: def method_splitter(request, ...

login_required decorator and urlresolver.reverse() in Django

I have several views with @login_required decorator. And I'm going to use django.core.urlresolvers.reverse() function for redirection to them. I wrote in urls.py: urlpatterns = ('myapp.views', '^(?P<key>[-a-zA-Z0-9]+)/some-operation/$', 'some_operation'), ) and in views.py: return HttpResponseRedirect( reverse('myapp.views....

Why on earth do I have to pass RequestContext in all of my responses?

I want to highlight the current page in the navigation menu. Obviously I need to give the menu links a class like 'active' when you are on their page. This is a classic problem and I've seen many solutions proposed. My problem is I hate all of them and consider none of them to be very DRY. For example: @register.simple_tag def active(re...

How can I pass data to any template from any view in Django?

Like a good little coder, all of my Django templates inherit from a base.html. Now I would like to add some functionality to the base to always show some interesting things. Some user statistics, or random posts, or feeds, etc. All of my views look like this: def viewname(request) : template_vales = {} // Stuff return rend...

Keeping Django Views DRY

I have some code that gets the current logged in user. userID = request.session.get("_auth_user_id") if userID: loggedin_user = User.objects.get(pk=int(userID)) else: loggedin_user = None I want the username to show up on every page. At the moment I am putting the code in every view and passing the user object to e...

how do i filter an itertools chain() result?

in my views, if i import an itertools module: from itertools import chain and i chain some objects with it: franktags = Frank.objects.order_by('date_added').reverse().filter(topic__exact='art') amytags = Amy.objects.order_by('date_added').reverse().filter(topic__exact='art') timtags = Tim.objects.order_by('date_added').reverse().fi...