Code

Opened 5 years ago

Closed 4 years ago

#10328 closed (fixed)

get_script_path and request.path_info undocumented

Reported by: leovitch Owned by: anonymous
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

As near as I can tell by reading the code, the new django.path support for mod_python in 1.0 has a couple implications for code in cases where the application needs to do URL manipulations itself (I found this while testing django-cms). While the normal urlresolver takes care of anything done through reverse() or the url template tag, code that does its own manipulations on URLs need to:

  • Call get_script_prefix() to get the script prefix portion to use when constructing an URL from scratch
  • Use request.path_info rather than request.path when parsing an URL

Unfortunately, both of those seem to be undocumented (at least, I can't find the documentation). Shouldn't they be described?

Purely in the spirit of being helpful rather than just complaining, below is my understanding of what the documentation would be -- but I didn't write the original code, so please disregard/correct/wordsmith this as appropriate.

Leo


get_script_prefix() would be described under URL Dispatcher > Utility Methods as follows.

Normally, you should always use reverse() or permalink() to define URLs within your application. However, if your application constructs part of the URL hierarchy itself you may occasionally need to generate URLs. In that case, you need to be able to find the base URL of the Django project within its web server (normally, the urlresolver takes care of this for you). In that case, you can call get_script_prefix(), which will return the script prefix portion of the URL for your Django project. If your Django project is at the root of its webserver, this is always '/', but it can be changed for instance by using the django.root option in the modpython handler under Apache.

The path_info attribute of a request object would be documented under Request and Response Objects > HTTPRequest objects > Attributes as follows:

HttpRequest.path_info

Under some web server configurations, the path to the page after the host name is split up into a script prefix portion and a path info portion (this happens, for example, when using the modpython handler from Apache). The path_info attribute always contains the path_info portion of the path, no matter what web server is being used. Using this instead of path can make your code much easier to move between web servers, since you won't need to care what your URL within the overall web server is.

Attachments (3)

request-response.diff (1.2 KB) - added by leovitch 5 years ago.
diff for docs/ref/request-reponse.txt
urls.diff (1.3 KB) - added by leovitch 5 years ago.
diff for docs/topics/http/urls.txt
10328.diff (2.8 KB) - added by timo 4 years ago.
combining existing diffs into a single patch + minor edits

Download all attachments as: .zip

Change History (11)

comment:1 Changed 5 years ago by leovitch

  • Component changed from Uncategorized to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by dc

  • Owner changed from nobody to anonymous
  • Status changed from new to assigned

Accepting as improved version of #8650

comment:3 Changed 5 years ago by SmileyChris

  • Summary changed from get_script_path and request.path_info undocumented? to get_script_path and request.path_info undocumented
  • Triage Stage changed from Unreviewed to Accepted

This would be more helpful as a patch. Want to try providing that, leovitch? (ask in irc if you need assistance)

comment:4 Changed 5 years ago by leovitch

How's this?

--- docs/ref/request-response.txt	(revision 9904)
+++ docs/ref/request-response.txt	(working copy)
@@ -37,6 +37,25 @@
 
    Example: ``"/music/bands/the_beatles/"``
 
+.. attribute:: HttpRequest.path_info
+
+   Under some web server configurations, 
+   the portion of the URL after the host name is split up  
+   into a script prefix portion and a path info portion  
+   (this happens, for example, when using the ``django.root`` option
+   with the
+   :ref:`modpython handler from Apache <howto-deployment-modpython>`).  
+   The ``path_info`` attribute always contains the  
+   path info portion of the path,  
+   no matter what web server is being used.  
+   Using this instead of ``path`` can make your code  
+   much easier to move between test and deployment servers. 
+
+   Example: If the ``django.root`` for your application is set to 
+   ``"/minfo"``, then ``path`` might be 
+   ``"/minfo/music/bands/the_beatles/"`` but ``path_info`` 
+   would be ``"/music/bands/the_beatles/"``.
+
 .. attribute:: HttpRequest.method
 
     A string representing the HTTP method used in the request. This is
--- docs/topics/http/urls.txt	(revision 9904)
+++ docs/topics/http/urls.txt	(working copy)
@@ -640,3 +640,28 @@
 The :func:`django.db.models.permalink` decorator is useful for writing short
 methods that return a full URL path. For example, a model's
 ``get_absolute_url()`` method. See :func:`django.db.models.permalink` for more.
+
+get_script_prefix()
+-------------------
+
+.. versionadded:: 1.0
+
+Normally, you should always use 
+:func:`~django.core.urlresolvers.reverse` or 
+:func:`~django.db.models.permalink` to define URLs 
+within your application. 
+However, if your application constructs 
+part of the URL hierarchy itself you may occasionally 
+need to generate URLs. 
+In that case, you need to be able to find the base URL 
+of the Django project within its web server 
+(normally, :func:`~django.core.urlresolvers.reverse` 
+takes care of this for you). 
+In that case, you can call ``get_script_prefix()``, 
+which will return the script prefix portion of the URL 
+for your Django project. 
+If your Django project is at the root of its webserver, 
+this is always ``"/"``, but it can be changed, for instance 
+by using the ``django.root`` (see 
+:ref:`How to use Django with Apache and mod_python <howto-deployment-modpython>`).
+

Changed 5 years ago by leovitch

diff for docs/ref/request-reponse.txt

Changed 5 years ago by leovitch

diff for docs/topics/http/urls.txt

comment:5 Changed 5 years ago by SmileyChris

  • Cc mtredinnick added
  • Has patch set
  • Version changed from 1.0 to SVN

malcolm may want to work on the wording, but it's a good addition. Thanks leovitch.

comment:6 Changed 5 years ago by mtredinnick

  • Cc mtredinnick removed

Changed 4 years ago by timo

combining existing diffs into a single patch + minor edits

comment:7 Changed 4 years ago by timo

  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 4 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [13772]) Documented how to manually piece together URLs with get_script_name().

Thanks to timo and leovitch for the patch. Fixed #10328.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.