本文整理汇总了Python中django.core.urlresolvers.NoReverseMatch方法的典型用法代码示例。如果您正苦于以下问题:Python urlresolvers.NoReverseMatch方法的具体用法?Python urlresolvers.NoReverseMatch怎么用?Python urlresolvers.NoReverseMatch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.urlresolvers
的用法示例。
在下文中一共展示了urlresolvers.NoReverseMatch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: context
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def context(self, context):
btns = []
for b in self.q_btns:
btn = {}
if 'model' in b:
model = self.get_model(b['model'])
if not self.user.has_perm("%s.view_%s" % (model._meta.app_label, model._meta.model_name)):
continue
btn['url'] = reverse("%s:%s_%s_%s" % (self.admin_site.app_name, model._meta.app_label,
model._meta.model_name, b.get('view', 'changelist')))
btn['title'] = model._meta.verbose_name
btn['icon'] = self.dashboard.get_model_icon(model)
else:
try:
btn['url'] = reverse(b['url'])
except NoReverseMatch:
btn['url'] = b['url']
if 'title' in b:
btn['title'] = b['title']
if 'icon' in b:
btn['icon'] = b['icon']
btns.append(btn)
context.update({'btns': btns})
示例2: block_top_navbar
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def block_top_navbar(self, context, nodes):
search_models = []
site_name = self.admin_site.name
if self.global_search_models == None:
models = self.admin_site._registry.keys()
else:
models = self.global_search_models
for model in models:
app_label = model._meta.app_label
if self.has_model_perm(model, "view"):
info = (app_label, model._meta.model_name)
if getattr(self.admin_site._registry[model], 'search_fields', None):
try:
search_models.append({
'title': _('Search %s') % capfirst(model._meta.verbose_name_plural),
'url': reverse('xadmin:%s_%s_changelist' % info, current_app=site_name),
'model': model
})
except NoReverseMatch:
pass
return nodes.append(loader.render_to_string('xadmin/blocks/comm.top.topnav.html', {'search_models': search_models, 'search_name': SEARCH_VAR}))
示例3: block_top_navmenu
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def block_top_navmenu(self, context, nodes):
add_models = []
site_name = self.admin_site.name
if self.global_add_models == None:
models = self.admin_site._registry.keys()
else:
models = self.global_add_models
for model in models:
app_label = model._meta.app_label
if self.has_model_perm(model, "add"):
info = (app_label, model._meta.model_name)
try:
add_models.append({
'title': _('Add %s') % capfirst(model._meta.verbose_name),
'url': reverse('xadmin:%s_%s_add' % info, current_app=site_name),
'model': model
})
except NoReverseMatch:
pass
nodes.append(
loader.render_to_string('xadmin/blocks/comm.top.topnav.html', {'add_models': add_models}))
示例4: get_redirect_url
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def get_redirect_url(self, *args, **kwargs):
"""
Return the URL redirect to. Keyword arguments from the
URL pattern match generating the redirect request
are provided as kwargs to this method.
"""
if self.url:
url = self.url % kwargs
elif self.pattern_name:
try:
url = reverse(self.pattern_name, args=args, kwargs=kwargs)
except NoReverseMatch:
return None
else:
return None
args = self.request.META.get('QUERY_STRING', '')
if args and self.query_string:
url = "%s?%s" % (url, args)
return url
示例5: handle_user_url
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def handle_user_url(self, menu_item):
"""
Evaluate user defined URL
:type menu_item: ChildItem or ParentItem
"""
if callable(menu_item.url):
menu_item.url = menu_item.url(self.request, self.context)
return menu_item
if '/' in menu_item.url:
return menu_item
from django.core.urlresolvers import reverse, NoReverseMatch
try:
menu_item.url = reverse(menu_item.url, current_app=self.current_app)
menu_item._url_name = menu_item.url
except NoReverseMatch:
menu_item.url = '#no-reverse-match'
return menu_item
示例6: test_followup_view_rendering
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def test_followup_view_rendering(self):
from django.core.urlresolvers import NoReverseMatch
for url in urls.urlpatterns:
if url.name in ['new-followup', 'followup']:
continue
# all the URLs have either one parameter or none. Try one
# parameter first; if that fails, try with none.
try:
self.verify_rendering(url.name, (1,))
except NoReverseMatch:
self.verify_rendering(url.name)
for fu_type in FU_TYPES:
self.verify_rendering('new-followup', (1, fu_type))
# TODO: build in checks for 'followup' once the objects exist.
# for model in ['General', 'Lab']:
# self.verify_rendering('followup',
# kwargs={"pk": 1, "model": model})
示例7: render
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def render(self, context):
args = [arg.resolve(context) for arg in self.args]
kwargs = dict([
(smart_str(k, 'ascii'), v.resolve(context))
for k, v in self.kwargs.items()])
view_name = self.view_name.resolve(context)
urlconf = self.urlconf.resolve(context)
try:
url = do_app_reverse(
view_name, urlconf, args=args, kwargs=kwargs,
current_app=context.current_app)
except NoReverseMatch:
if self.asvar is None:
raise
url = ''
if self.asvar:
context[self.asvar] = url
return ''
else:
return url
示例8: block_top_navbar
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def block_top_navbar(self, context, nodes):
search_models = []
site_name = self.admin_site.name
if self.global_search_models == None:
models = self.admin_site._registry.keys()
else:
models = self.global_search_models
for model in models:
app_label = model._meta.app_label
if self.has_model_perm(model, "view"):
info = (app_label, model._meta.model_name)
if getattr(self.admin_site._registry[model], 'search_fields', None):
try:
search_models.append({
'title': _('Search %s') % capfirst(model._meta.verbose_name_plural),
'url': reverse('xadmin:%s_%s_changelist' % info, current_app=site_name),
'model': model
})
except NoReverseMatch:
pass
nodes.append(loader.render_to_string('xadmin/blocks/comm.top.topnav.html', {'search_models': search_models, 'search_name': SEARCH_VAR}))
示例9: handle_redirect_to_login
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def handle_redirect_to_login(request, **kwargs):
login_url = kwargs.get("login_url")
redirect_field_name = kwargs.get("redirect_field_name")
next_url = kwargs.get("next_url")
if login_url is None:
login_url = settings.ACCOUNT_LOGIN_URL
if next_url is None:
next_url = request.get_full_path()
try:
login_url = urlresolvers.reverse(login_url)
except urlresolvers.NoReverseMatch:
if callable(login_url):
raise
if "/" not in login_url and "." not in login_url:
raise
url_bits = list(urlparse(login_url))
if redirect_field_name:
querystring = QueryDict(url_bits[4], mutable=True)
querystring[redirect_field_name] = next_url
url_bits[4] = querystring.urlencode(safe="/")
return HttpResponseRedirect(urlunparse(url_bits))
示例10: reverse
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
"""
If versioning is being used then we pass any `reverse` calls through
to the versioning scheme instance, so that the resulting URL
can be modified if needed.
"""
scheme = getattr(request, 'versioning_scheme', None)
if scheme is not None:
try:
return scheme.reverse(viewname, args, kwargs, request, format, **extra)
except NoReverseMatch:
# In case the versioning scheme reversal fails, fallback to the default implementation
pass
return _reverse(viewname, args, kwargs, request, format, **extra)
示例11: get_link_url
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def get_link_url(self, datum=None):
"""Returns the final URL based on the value of ``url``.
If ``url`` is callable it will call the function.
If not, it will then try to call ``reverse`` on ``url``.
Failing that, it will simply return the value of ``url`` as-is.
When called for a row action, the current row data object will be
passed as the first parameter.
"""
if not self.url:
raise NotImplementedError('A LinkAction class must have a '
'url attribute or define its own '
'get_link_url method.')
if callable(self.url):
return self.url(datum, **self.kwargs)
try:
if datum:
obj_id = self.table.get_object_id(datum)
return urlresolvers.reverse(self.url, args=(obj_id,))
else:
return urlresolvers.reverse(self.url)
except urlresolvers.NoReverseMatch as ex:
LOG.info('No reverse found for "%s": %s' % (self.url, ex))
return self.url
示例12: get_link_url
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def get_link_url(self, datum):
"""Returns the final value for the column's ``link`` property.
If ``allowed_data_types`` of this column is not empty and the datum
has an assigned type, check if the datum's type is in the
``allowed_data_types`` list. If not, the datum won't be displayed
as a link.
If ``link`` is a callable, it will be passed the current data object
and should return a URL. Otherwise ``get_link_url`` will attempt to
call ``reverse`` on ``link`` with the object's id as a parameter.
Failing that, it will simply return the value of ``link``.
"""
if self.allowed_data_types:
data_type_name = self.table._meta.data_type_name
data_type = getattr(datum, data_type_name, None)
if data_type and (data_type not in self.allowed_data_types):
return None
obj_id = self.table.get_object_id(datum)
if callable(self.link):
return self.link(datum)
try:
return urlresolvers.reverse(self.link, args=(obj_id,))
except urlresolvers.NoReverseMatch:
return self.link
示例13: active_link
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def active_link(context, viewnames, css_class=None, strict=None, *args, **kwargs):
"""
Renders the given CSS class if the request path matches the path of the view.
:param context: The context where the tag was called. Used to access the request object.
:param viewnames: The name of the view or views separated by || (include namespaces if any).
:param css_class: The CSS class to render.
:param strict: If True, the tag will perform an exact match with the request path.
:return:
"""
if css_class is None:
css_class = getattr(settings, 'ACTIVE_LINK_CSS_CLASS', 'active')
if strict is None:
strict = getattr(settings, 'ACTIVE_LINK_STRICT', False)
request = context.get('request')
if request is None:
# Can't work without the request object.
return ''
active = False
views = viewnames.split('||')
for viewname in views:
try:
path = reverse(viewname.strip(), args=args, kwargs=kwargs)
except NoReverseMatch:
continue
request_path = escape_uri_path(request.path)
if strict:
active = request_path == path
else:
active = request_path.find(path) == 0
if active:
break
if active:
return css_class
return ''
示例14: resolve_url
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def resolve_url(to, *args, **kwargs):
"""
Return a URL appropriate for the arguments passed.
The arguments could be:
* A model: the model's `get_absolute_url()` function will be called.
* A view name, possibly with arguments: `urlresolvers.reverse()` will
be used to reverse-resolve the name.
* A URL, which will be returned as-is.
"""
# If it's a model, use get_absolute_url()
if hasattr(to, 'get_absolute_url'):
return to.get_absolute_url()
if isinstance(to, Promise):
# Expand the lazy instance, as it can cause issues when it is passed
# further to some Python functions like urlparse.
to = force_text(to)
if isinstance(to, six.string_types):
# Handle relative URLs
if any(to.startswith(path) for path in ('./', '../')):
return to
# Next try a reverse URL resolution.
try:
return urlresolvers.reverse(to, args=args, kwargs=kwargs)
except urlresolvers.NoReverseMatch:
# If this is a callable, re-raise.
if callable(to):
raise
# If this doesn't "feel" like a URL, re-raise.
if '/' not in to and '.' not in to:
raise
# Finally, fall back and assume it's a URL
return to
示例15: ping_google
# 需要导入模块: from django.core import urlresolvers [as 别名]
# 或者: from django.core.urlresolvers import NoReverseMatch [as 别名]
def ping_google(sitemap_url=None, ping_url=PING_URL):
"""
Alerts Google that the sitemap for the current site has been updated.
If sitemap_url is provided, it should be an absolute path to the sitemap
for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this
function will attempt to deduce it by using urlresolvers.reverse().
"""
if sitemap_url is None:
try:
# First, try to get the "index" sitemap URL.
sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.index')
except urlresolvers.NoReverseMatch:
try:
# Next, try for the "global" sitemap URL.
sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap')
except urlresolvers.NoReverseMatch:
pass
if sitemap_url is None:
raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.")
if not django_apps.is_installed('django.contrib.sites'):
raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.")
Site = django_apps.get_model('sites.Site')
current_site = Site.objects.get_current()
url = "http://%s%s" % (current_site.domain, sitemap_url)
params = urlencode({'sitemap': url})
urlopen("%s?%s" % (ping_url, params))