本文整理匯總了Python中django.contrib.sites.models.Site.DoesNotExist方法的典型用法代碼示例。如果您正苦於以下問題:Python Site.DoesNotExist方法的具體用法?Python Site.DoesNotExist怎麽用?Python Site.DoesNotExist使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.sites.models.Site
的用法示例。
在下文中一共展示了Site.DoesNotExist方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: handle
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def handle(self, *args, **options):
"""Handle the command invocation."""
site_id = getattr(settings, 'SITE_ID', None)
site_name = getattr(settings, 'SITE_NAME', None)
site_domain = getattr(settings, 'SITE_DOMAIN', None)
if not site_id:
raise CommandError('No SITE_ID specified in project settings')
if not site_name:
raise CommandError('No SITE_NAME specified in project settings')
if not site_domain:
raise CommandError('No SITE_DOMAIN specified in project settings')
try:
site = Site.objects.get(pk=site_id)
except Site.DoesNotExist: # pylint: disable=no-member
raise CommandError('Site "%s" does not exist' % site_id)
site.name = site_name
site.domain = site_domain
site.save()
msg = 'Successfully configured site #%s; name: "%s"; domain: %s' % (site_id, site_name, site_domain)
self.stdout.write(self.style.SUCCESS(msg)) # pylint: disable=no-member
示例2: add_view
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def add_view(self, request, form_url='', extra_context=None):
site_id = request.GET.get('site_id', None)
if not site_id:
return self.select_site_view(request)
else:
try:
site_id = self.model._meta.pk.to_python(site_id)
site = Site.objects.get(pk=site_id)
except (Site.DoesNotExist, ValidationError, ValueError):
return self.select_site_view(request)
else:
try:
obj = self.model.objects.get(site=site)
change_url = reverse(
'admin:%s_%s_change' % self.get_model_info(), args=(obj.pk,),
current_app=self.admin_site.name)
msg = _('{0} for "{1}" already exists. You may edit it below.')\
.format(self.opts.verbose_name, site.domain)
self.message_user(request, msg)
return HttpResponseRedirect(change_url)
except self.model.DoesNotExist:
pass
return super(SettingsAdmin, self).add_view(request, form_url, extra_context)
示例3: changelist_view
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def changelist_view(self, request, extra_context=None):
sites_count = Site.objects.count()
if sites_count == 1:
site = Site.objects.get()
try:
obj = self.model.objects.get(site=site)
change_url = reverse(
'admin:%s_%s_change' % self.get_model_info(), args=(obj.pk,),
current_app=self.admin_site.name)
return HttpResponseRedirect(change_url)
except self.model.DoesNotExist:
add_url = '%s?site_id=%s' % (
reverse('admin:%s_%s_add' % self.get_model_info(),
current_app=self.admin_site.name), site.pk)
return HttpResponseRedirect(add_url)
else:
return super(SettingsAdmin, self).changelist_view(request, extra_context)
示例4: _get_context
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def _get_context(self, context_list):
try:
data = self._model_to_dict(Site.objects.get_current())
except Site.DoesNotExist:
data = {}
for context in context_list:
if isinstance(context, dict):
data.update(context)
elif hasattr(context, '_meta'):
data.update(self._model_to_dict(context))
data.update({self._get_context_module_name(context): context})
if settings.DEBUG and SHOW_CONTEXT:
pprint.pprint(data)
return data
示例5: handle
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def handle(self, *args, **options):
queryset = Referral.objects.filter(site__isnull=True)
count = queryset.count()
try:
site = Site.objects.get(id=options['site_id'])
except Site.DoesNotExist:
msg = 'A valid Site ID must be specified!'
self.stderr.write(msg)
raise CommandError(msg)
if options['commit']:
self.stdout.write('Associating [{}] referrals with site [{}]...'.format(count, site))
queryset.update(site=site)
self.stdout.write('Done.')
else:
msg = 'This has been an example operation. If the --commit flag had been included, the command ' \
'would have associated [{}] referrals with site [{}].'.format(count, site)
self.stdout.write(msg)
示例6: handle
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def handle(self, *args, **options):
queryset = Basket.objects.filter(site__isnull=True)
count = queryset.count()
try:
site = Site.objects.get(id=options['site_id'])
except Site.DoesNotExist:
raise CommandError('A valid Site ID must be specified!')
if options['commit']:
self.stderr.write('Associating [{}] baskets with site [{}]...'.format(count, site))
queryset.update(site=site)
self.stderr.write('Done.')
else:
msg = 'This has been an example operation. If the --commit flag had been included, the command ' \
'would have associated [{}] baskets with site [{}].'.format(count, site)
self.stderr.write(msg)
示例7: handle
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def handle(self, *args, **options):
site_id = options.get('site_id')
site_domain = options.get('site_domain')
site_name = options.get('site_name')
site_theme = options.get('site_theme')
try:
site = Site.objects.get(id=site_id)
except Site.DoesNotExist:
site, site_created = Site.objects.get_or_create(domain=site_domain)
if site_created:
logger.info('Site created with domain %s', site_domain)
site.domain = site_domain
if site_name:
site.name = site_name
site.save()
_, created = SiteTheme.objects.update_or_create(
site=site,
defaults={
'theme_dir_name': site_theme,
}
)
logger.info('Site Theme %s with theme "%s"', "created" if created else "updated", site_theme)
示例8: site_name
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def site_name(request):
site_id = getattr(settings, 'SITE_ID', None)
try:
site = Site.objects.get(pk=site_id)
except Site.DoesNotExist:
site_name = 'example.org'
else:
site_name = site.name
return {'SITE_NAME': site_name}
示例9: handle
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def handle(self, *args, **options):
"""gets data from WordPress site"""
# TODO: refactor these with .get
if 'username' in options:
self.username = options['username']
else:
self.username = None
if 'password' in options:
self.password = options['password']
else:
self.password = None
self.xml_path = options.get('xml')
self.url = options.get('url')
try:
blog_index = BlogIndexPage.objects.get(
title__icontains=options['blog_index'])
except BlogIndexPage.DoesNotExist:
raise CommandError("Incorrect blog index title - have you created it?")
if self.url == "just_testing":
with open('test-data.json') as test_json:
posts = json.load(test_json)
elif self.xml_path:
try:
import lxml
from blog.wp_xml_parser import XML_parser
except ImportError as e:
print("You must have lxml installed to run xml imports."
" Run `pip install lxml`.")
raise e
self.xml_parser = XML_parser(self.xml_path)
posts = self.xml_parser.get_posts_data()
else:
posts = self.get_posts_data(self.url)
self.should_import_comments = options.get('import_comments')
self.create_blog_pages(posts, blog_index)
示例10: create_user
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def create_user(self, author):
username = author['username']
first_name = author['first_name']
last_name = author['last_name']
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
user = User.objects.create_user(
username=username, first_name=first_name, last_name=last_name)
return user
示例11: __init__
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def __init__(self, state=None, request=None, *args, **kwargs):
from .conf import config
self.config = config
extra_scopes = self.config.MICROSOFT_AUTH_EXTRA_SCOPES
try:
current_site = Site.objects.get_current(request)
except Site.DoesNotExist:
current_site = Site.objects.first()
domain = current_site.domain
path = reverse("microsoft_auth:auth-callback")
scope = " ".join(self.SCOPE_MICROSOFT)
if self.config.MICROSOFT_AUTH_LOGIN_TYPE == LOGIN_TYPE_XBL:
scope = " ".join(self.SCOPE_XBL)
scope = "{} {}".format(scope, extra_scopes).strip()
scheme = get_scheme(request, self.config)
super().__init__(
self.config.MICROSOFT_AUTH_CLIENT_ID,
scope=scope,
state=state,
redirect_uri="{0}://{1}{2}".format(scheme, domain, path),
*args,
**kwargs
)
示例12: microsoft
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def microsoft(request):
""" Adds global template variables for microsoft_auth """
login_type = None
if config.MICROSOFT_AUTH_LOGIN_TYPE == LOGIN_TYPE_XBL:
login_type = _("Xbox Live")
else:
login_type = _("Microsoft")
if config.DEBUG: # pragma: no branch
try:
current_domain = Site.objects.get_current(request).domain
except Site.DoesNotExist:
logger.warning(
"\nWARNING:\nThe domain configured for the sites framework "
"does not match the domain you are accessing Django with. "
"Microsoft authentication may not work.\n"
)
else:
do_warning = get_scheme(
request
) == "http" and not current_domain.startswith("localhost")
if do_warning: # pragma: no branch
logger.warning(
"\nWARNING:\nYou are not using HTTPS. Microsoft "
"authentication only works over HTTPS unless the hostname "
"for your `redirect_uri` is `localhost`\n"
)
# initialize Microsoft client using CSRF token as state variable
signer = TimestampSigner()
state = signer.sign(get_token(request))
microsoft = MicrosoftClient(state=state, request=request)
auth_url = microsoft.authorization_url()[0]
return {
"microsoft_login_enabled": config.MICROSOFT_AUTH_LOGIN_ENABLED,
"microsoft_authorization_url": mark_safe(auth_url), # nosec
"microsoft_login_type_text": login_type,
}
示例13: _get_msg_with_track
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def _get_msg_with_track(self):
message = self._message
if defaults.TRACK_ENABLE is False:
return message
if ENABLE_LOGGING and self._template.enable_log:
try:
domain = Site.objects.get_current().domain
encrypted = signing.dumps(self._log_id, compress=True)
path = reverse('db-mail-tracker', args=[encrypted])
message += defaults.TRACK_HTML % {
'url': 'http://%s%s' % (domain, path)}
except (Site.DoesNotExist, NoReverseMatch):
pass
return message
示例14: call_first_site_post_save
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def call_first_site_post_save(apps, **kwargs):
try:
site = Site.objects.get(id=getattr(settings, 'SITE_ID', 1))
except Site.DoesNotExist:
pass
else:
site.save()
示例15: handle
# 需要導入模塊: from django.contrib.sites.models import Site [as 別名]
# 或者: from django.contrib.sites.models.Site import DoesNotExist [as 別名]
def handle(self, *args, **options):
try:
site = Site.objects.get(id=options['id'])
site.name = options['name']
site.domain = options['domain']
except Site.DoesNotExist:
site = Site.objects.create(id=options['id'], name=options['name'], domain=options['domain'])
site.save()
self.stdout.write('Created or updated site {}.'.format(options['id']))