Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django URL Detail

Tags:

url

django

I have to assign to work on one Django project. I need to know about the URL say, http://.... Since with ‘urls.py’ we indeed have ‘raw’ information. How I come to know about the complete URL name; mean with

http+domain+parameters

Amit.

like image 395
Amit Yadav Avatar asked Jan 26 '26 20:01

Amit Yadav


2 Answers

Look at this snippet :
http://djangosnippets.org/snippets/1197/

I modified it like this :

from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site

def site_info(request):
    site_info = {'protocol': request.is_secure() and 'https' or 'http'}
    if Site._meta.installed:
        site_info['domain'] = Site.objects.get_current().domain
        site_info['name'] = Site.objects.get_current().name
    else:
        site_info['domain'] = RequestSite(request).domain
        site_info['name'] = RequestSite(request).name
    site_info['root'] = site_info['protocol'] + '://' + site_info['domain']
    return {'site_info':site_info}

The if/else is because of different versions of Django Site API

This snippet is actually a context processor, so you have to paste it in a file called context_processors.py in your application, then add to your settings :

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
    'name-of-your-app.context_processors.site_info',
)

The + is here to take care that we d'ont override the possible default context processor set up by django, now or in the future, we just add this one to the tuple.

Finally, make sure that you use RequestContext in your views when returning the response, and not just Context. This explained here in the docs.
It's just a matter of using :

def some_view(request):
    # ...
    return render_to_response('my_template.html',
                          my_data_dictionary,
                          context_instance=RequestContext(request))
like image 97
Dominique Guardiola Avatar answered Jan 29 '26 11:01

Dominique Guardiola


HTTPS status would be handled differently by different web servers.

For my Nginx reverse proxy to Apache+WSGI setup, I explicitly set a header that apache (django) can check to see if the connection is secure.

This info would not be available in the URL but in your view request object.

django uses request.is_secure() to determine if the connection is secure. How it does so depends on the backend.
http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.is_secure

For example, for mod_python, it's the following code:

def is_secure(self):
    try:
        return self._req.is_https()
    except AttributeError:
        # mod_python < 3.2.10 doesn't have req.is_https().
        return self._req.subprocess_env.get('HTTPS', '').lower() in ('on', '1')

If you are using a proxy, you will probably find it useful that HTTP Headers are available in HttpRequest.META

http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META

Update: if you want to log every secure request, use the above example with a middleware

class LogHttpsMiddleware(object):
    def process_request(self, request):
            if request.is_secure():
                 protocol = 'https'
            else:
                 protocol = 'http'
            print "%s://www.mydomain.com%s" % (protocol, request.path)

Add LogHttpsMiddleware to your settings.py MIDDLEWARE_CLASSES

like image 26
Yuji 'Tomita' Tomita Avatar answered Jan 29 '26 10:01

Yuji 'Tomita' Tomita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!