django-urls

Dynamic SEO-friendly URLs

人走茶凉 提交于 2019-12-05 13:13:01
I'd like to deploy dynamic URL's for my app in two ways: when viewing available vehicle, I get a link like: http://www.url.com/2006-Acura-MDX-Technology-Package I also have a filter page, so here, the URL will change according to the filters selected like: http://www.url.com/2007-Nissan or http://www.url.com/2007-Nissan-Maxima and so on depending on the filters the user has chosen. What's the best way to go about this? EDIT 1 This now works def get_absolute_url(self): return u'%s-%s-%s-%s-%s' % (self.common_vehicle.year.year, self.common_vehicle.series.model.manufacturer, self.common_vehicle

django getting the absolute path of a FileField

大城市里の小女人 提交于 2019-12-05 12:46:51
问题 I am trying to retrieve the absolute path (starting with http://) while calling a FileField at the template. How can I achieve this ? ie: {{fl.uploadedfile}} -> returns relative path like media/uploads/ while I want This Cheers 回答1: The Django File object provides several convenience functions/attributes, one of which is the URL associated with the file. In this case, as you pointed out, you're looking for the url attribute. 回答2: Just found the answer: adding .url fixes this issue fixes it

django form got multiple values for keyword argument

余生长醉 提交于 2019-12-05 08:35:23
问题 I have a simple model as follows: RATING_CHOICES = zip(range(1, 6), range(1, 6)) class Rating(models.Model): value = models.IntegerField(choices=RATING_CHOICES) additional_note = models.TextField(null=True, blank=True) from_user = models.ForeignKey(User, related_name='from_user') to_user = models.ForeignKey(User, related_name='to_user') shared_object = models.ForeignKey(ObjectDetail, null=True, blank=True) dtobject = models.DateTimeField(auto_now_add=True) From the above model I generate a

Django HttpResponseRedirect vs render_to_response - how to get a login form to behave the way I need it to

风格不统一 提交于 2019-12-05 07:48:18
问题 I've already checked out the following stackoverflow question regarding the difference between HttpResponse, HttpResponseRedirect, and render_to_response, as well as having gone through the official django docs, but I'm really uncertain how best to get the functionality I'm looking to create. Right now I have an index.html with a login function (as seen in the views.py below) where the render_to_response that brings me to portal/index.html . However, as urls.py (see below) dictates, the url

Django Context Processors: Is it possible to access current context in ContextProcessor?

拜拜、爱过 提交于 2019-12-05 02:20:35
Is there a way I can access current context passed by view in custom context processor so I can add missing variable if I want rather than overriding existing variable ? What I'm trying to Achieve: I construct my URL's like this /city_slug/ and I want to check if city variable already exist in context, otherwise I want to add city to my context (may be using the last used city stored in session variable otherwise default to some city and may be even set session variable for next use.) I think this is very common problem, How do you guys solve it ? You cannot access the current context from

How can you dispatch on request method in Django URLpatterns?

爷,独闯天下 提交于 2019-12-04 23:41:50
It's clear how to create a URLPattern which dispatches from a URL regex: (r'^books/$', books), where books can further dispatch on request method: def books(request): if request.method == 'POST': ... else: ... I'd like to know if there is an idiomatic way to include the request method inside the URLPattern, keeping all dispatch/route information in a single location, such as: (r'^books/$', GET, retrieve-book), (r'^books/$', POST, update-books), (r'^books/$', PUT, create-books), The reason it's done as a single view method is that you're usually rendering some kind of page content as context

Raise 404 and continue the URL chain

谁说胖子不能爱 提交于 2019-12-04 22:16:24
I've got a URLs pattern like this: urlpatterns = ( url(r'^$', list_titles, name='list'), url(r'^(?P<tag>[a-z\-0-9]+?)/$', list_titles, name='filtered-list'), url(r'^(?P<title>\S+?)/$', show_title, name='title'), ) The filtered-list and title match the same things. If there is an available list of things matching the tag in filtered-list , I want list_titles to fire off. But if there isn't a matching tag , I want to bubble that back to the URL processor so show_title fires off. If there's no matching title, I'll raise a proper 404 there. I know I can do this from inside the view...but it's a

Override Django Admin URLs for Specific Model?

梦想与她 提交于 2019-12-04 21:45:08
问题 First a little background: I have an Event model that has various event_type s. I want to break one of those event types, 'Film', into it's own admin. I have the basic functionality in place: a proxy model inheriting from Event , named Film , a custom manager for that proxy model that filters it to only 'film' event types, and it's own ModelAdmin. The problem is with the reverse. I now need to filter out films from the main Event admin. I don't want to alter the Event model or its default

Is there any way that I can write URL in django using annotations in python

落爺英雄遲暮 提交于 2019-12-04 20:15:46
I am from the Java Hibernate and Symfony2 background where I used to write the routing within the controller on the top of function like this: /** * @Route("/blog") */ class PostController extends Controller { I know its not available in Django but is there any way I can code some decorator etc. so that I can mention the URL like this: @URL("/mytest") class myView(): pass While it would be very undjangonic , you could try something like this: project/ decorators.py views.py urls.py # decorators.py from django.conf import settings from django.utils.importlib import import_module from django

Django view getting called twice (double GET request)

懵懂的女人 提交于 2019-12-04 17:21:57
I'm creating a classifieds website in Django. A single view function handles global listings, city-wise listings, barter-only global listings and barter-only city-wise listings. This view is called ads . The url patterns are written in the following order (note that each has a unique name although it's tied to the same ads view): urlpatterns = patterns('', url(r'^buy_and_sell/$', ads,name='classified_listing'), url(r'^buy_and_sell/barter/$', ads,name='barter_classified_listing'), url(r'^buy_and_sell/barter/(?P<city>[\w.@+-]+)/$', ads,name='city_barter_classified_listing'), url(r'^buy_and_sell/