On Friday 13 January 2017 13:15:43 Max wrote:
> Vijay Khemlani, i think my question have not a lot of details. More
> 1) Django site framework (works with request.get_host() without
> Site_id). I can get the current site with
> Site.objects.get_current(*request*), shortcuts (get_current_site) or
> through model. But i have only one model with site relationship.
> 
> 2) Django class-based view.
> 
> 3) For example, template
> 
> > {{obj.get_url}} <- error Site does not exists [set site_id or use
> > 1)]
> 
> Model
> class Model(models.Model):
>       def get_absolute_url(self):
> 
>     return 'http://{}{}'.format(Site.objects.get_current(????).domain,
> reverse(viewname='product',
>                                         args=[self.slug]))
> 
> 
> Thanks, also i do not want to change site_id dynamically (thread) or
> use site through model-model.

I wrote this a long time ago. Maybe it's what you need:
*from *django.conf *import *settings
*from *django.contrib.sites.models *import *Site

*from *django.core.urlresolvers *import *reverse

*from *django.db *import *models

*class PublicModel(*models.Model*):    *"""A model that has a place on the site.

    Specifically, this adds the following method to the model:    - 
get_absolute_url(): the 
url from the base of the site to the detail view      of the model. It is 
implemented as 
the get_absolute_url() method so      that the Django admin will provide a 
"View on 
site" link.      The default implementation returns the url associated with:    
    
appname.views.modelname      and is decorated with the permalink decorator.    
Additionally the following property is added (implemented as a getter and    
setter):    - 
fully_qualified_url: A url that specifies the server and protocol as well      
as the 
absolute url. It is intended to be used for:      - out of band communications 
like email;      
- setting or changing the protocol security;      - changing or setting the 
associated 
site;    The general purpose of the model is to make it easier to get this    
information 
in a template and handle it in a view.    """    _site_id *= None    *_protocol 
*= None

    @models.permalink    def get_absolute_url(*self*):        *model_name *= 
*self._meta.object_name.lower*()        *viewname *= 
'{}_detail'*.format*(*model_name*)        return *viewname, [*/str/(*self.pk*)*]

    absolute_url *= /property/(*get_absolute_url, doc*="Absolute url")

    def get_fully_qualified_url(*self*):        *site_id *= *self._site_id *or 
/getattr/(*settings, *'SITE_ID'*, 1*)        *protocol *= *self._protocol *or 
'http'        try:            
*site *= *Site.objects.get*(*pk*=*site_id*)        except *Site.DoesNotExist*:  
          
raise *ImproperlyConfigured*(                'Site with id {} does not 
exist'*.format*(*site_id*))        *url *= '{}://{}{}'*.format*(*protocol, 
site.domain, 
self.absolute_url*)        return *url

    *def set_fully_qualified_url(*self, protocol, site_id_or_obj*=None**):      
  if 
*site_id_or_obj *is None:            *site_id *= *self._site_id *or 
/getattr/(*settings, 
*'SITE_ID'*, 1*)            *site *= *Site.objects.get*(*pk*=*site_id*)        
elif 
/isinstance/(*site_id_or_obj, Site*):            *site *= *site_id_or_obj       
 *else:            
*site *= *Site.objects.get*(*pk*=*site_id_or_obj*)        *self._site_id *= 
*site.pk        
self._protocol *= *protocol

    fully_qualified_url *= /property/(        *get_fully_qualified_url, 
set_fully_qualified_url,        
doc*="Fully qualified url (includes protocol and domain)"    )

    def get_list_url(*self*):        *viewname *= 
'{}_list'*.format*(*self._meta.object_name.lower*())        return 
*reverse*(*viewname*)

    /@/classmethod    def set_site_for_model(*cls, site_id_or_obj*):        if 
/isinstance/(*site_id_or_obj, Site*):            *cls._site_id *= 
*site_id_or_obj.pk        *else:            
*cls._site_id *= *site_id_or_obj

    */@/classmethod    def set_protocol_for_model(*cls, protocol*):        
*cls._protocol 
*= *protocol

    *class Meta:        *abstract *= True


*
-- 
Melvyn Sopacua

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3367638.qDexUdUU2D%40devstation.
For more options, visit https://groups.google.com/d/optout.

Reply via email to