本文整理匯總了Python中django.shortcuts.resolve_url方法的典型用法代碼示例。如果您正苦於以下問題:Python shortcuts.resolve_url方法的具體用法?Python shortcuts.resolve_url怎麽用?Python shortcuts.resolve_url使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.shortcuts
的用法示例。
在下文中一共展示了shortcuts.resolve_url方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: contact_detail
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def contact_detail(request, pk):
if request.method == 'POST':
data = (request.POST.get(key) for key in ('name', 'fone', 'email'))
contact = Contact.objects.get(pk=pk)
contact.name, contact.fone, contact.email = data
contact.save()
else:
contact = get_object_or_404(Contact, pk=pk)
response = dict(
name=contact.name,
avatar=contact.avatar(),
email=contact.email,
phone=contact.fone,
url=resolve_url('contact-details', pk=contact.pk)
)
return JsonResponse(response)
示例2: user_passes_test
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def user_passes_test(test_func: Callable[[HttpResponse], bool], login_url: Optional[str]=None,
redirect_field_name: str=REDIRECT_FIELD_NAME) -> Callable[[ViewFuncT], ViewFuncT]:
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
def decorator(view_func: ViewFuncT) -> ViewFuncT:
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse:
if test_func(request):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urllib.parse.urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = urllib.parse.urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
return redirect_to_login(
path, resolved_login_url, redirect_field_name)
return cast(ViewFuncT, _wrapped_view) # https://github.com/python/mypy/issues/1927
return decorator
示例3: redirect_to_login
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def redirect_to_login(next, login_url=None,
redirect_field_name=REDIRECT_FIELD_NAME):
"""
Redirects the user to the login page, passing the given 'next' page
"""
resolved_url = resolve_url(login_url or settings.LOGIN_URL)
login_url_parts = list(urlparse(resolved_url))
if redirect_field_name:
querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next
login_url_parts[4] = querystring.urlencode(safe='/')
return HttpResponseRedirect(urlunparse(login_url_parts))
# 4 views for password reset:
# - password_reset sends the mail
# - password_reset_done shows a success message for the above
# - password_reset_confirm checks the link the user clicked and
# prompts for a new password
# - password_reset_complete shows a success message for the above
示例4: form_valid
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def form_valid(self, form):
data = form.cleaned_data
domain = data['domain_base']
domain_is_whitelisted = check_valid_tld(domain)
if not domain_is_whitelisted:
messages.info(
self.request,
"Sorry, but to limit the cost of running this service, we have not enabled searching this domain name (%s)." % domain
)
return HttpResponseRedirect(resolve_url('home'))
search_done = search_bing(domain)
if not search_done:
messages.info(
self.request,
"This domain has already been requested today! Here is what we've gathered."
)
else:
messages.info(
self.request,
"Gathering results now. They will be displayed shortly."
)
return HttpResponseRedirect(
resolve_url('domain_result', domain)
)
示例5: get_redirect_url
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def get_redirect_url(request):
"""Redirects to referring page, or CAS_REDIRECT_URL if no referrer is
set.
"""
next_ = request.GET.get(REDIRECT_FIELD_NAME)
if not next_:
redirect_url = resolve_url(django_settings.CAS_REDIRECT_URL)
if django_settings.CAS_IGNORE_REFERER:
next_ = redirect_url
else:
next_ = request.META.get('HTTP_REFERER', redirect_url)
prefix = urllib_parse.urlunparse(
(get_protocol(request), request.get_host(), '', '', '', ''),
)
if next_.startswith(prefix):
next_ = next_[len(prefix):]
return next_
示例6: get_next_page
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def get_next_page(self):
if self.next_page is not None:
next_page = resolve_url(self.next_page)
elif settings.LOGOUT_REDIRECT_URL:
next_page = resolve_url(settings.LOGOUT_REDIRECT_URL)
else:
next_page = self.next_page
if (self.redirect_field_name in self.request.POST or
self.redirect_field_name in self.request.GET):
next_page = self.request.POST.get(
self.redirect_field_name,
self.request.GET.get(self.redirect_field_name)
)
url_is_safe = is_safe_url(
url=next_page,
allowed_hosts=self.get_success_url_allowed_hosts(),
require_https=self.request.is_secure(),
)
# Security check -- Ensure the user-originating redirection URL is
# safe.
if not url_is_safe:
next_page = self.request.path
return next_page
示例7: redirect_to_login
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Redirect the user to the login page, passing the given 'next' page.
"""
resolved_url = resolve_url(login_url or settings.LOGIN_URL)
login_url_parts = list(urlparse(resolved_url))
if redirect_field_name:
querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next
login_url_parts[4] = querystring.urlencode(safe='/')
return HttpResponseRedirect(urlunparse(login_url_parts))
# 4 views for password reset:
# - password_reset sends the mail
# - password_reset_done shows a success message for the above
# - password_reset_confirm checks the link the user clicked and
# prompts for a new password
# - password_reset_complete shows a success message for the above
示例8: password_reset_complete
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def password_reset_complete(request,
template_name='registration/password_reset_complete.html',
extra_context=None):
warnings.warn("The password_reset_complete() view is superseded by the "
"class-based PasswordResetCompleteView().",
RemovedInDjango21Warning, stacklevel=2)
context = {
'login_url': resolve_url(settings.LOGIN_URL),
'title': _('Password reset complete'),
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context)
# Class-based password reset views
# - PasswordResetView sends the mail
# - PasswordResetDoneView shows a success message for the above
# - PasswordResetConfirmView checks the link the user clicked and
# prompts for a new password
# - PasswordResetCompleteView shows a success message for the above
示例9: dispatch
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def dispatch(self, request):
redirect_to = request.GET.get(REDIRECT_FIELD_NAME, REDIRECT_URL)
# Make sure we're not redirecting to other sites
if not is_safe_url(url=redirect_to, host=request.get_host()):
redirect_to = resolve_url(REDIRECT_URL)
if request.is_sudo():
return HttpResponseRedirect(redirect_to)
if request.method == "GET":
request.session[REDIRECT_TO_FIELD_NAME] = redirect_to
context = {
"form": self.form_class(request.user, request.POST or None),
"request": request,
REDIRECT_FIELD_NAME: redirect_to,
}
if self.handle_sudo(request, redirect_to, context):
return self.grant_sudo_privileges(request, redirect_to)
if self.extra_context is not None:
context.update(self.extra_context)
return TemplateResponse(request, self.template_name, context)
示例10: redirect_to_sudo
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def redirect_to_sudo(next_url, sudo_url=None):
"""
Redirects the user to the login page, passing the given 'next' page
"""
if sudo_url is None:
sudo_url = URL
try:
# django 1.10 and greater can't resolve the string 'sudo.views.sudo' to a URL
# https://docs.djangoproject.com/en/1.10/releases/1.10/#removed-features-1-10
sudo_url = import_string(sudo_url)
except (ImportError, ImproperlyConfigured):
pass # wasn't a dotted path
sudo_url_parts = list(urlparse(resolve_url(sudo_url)))
querystring = QueryDict(sudo_url_parts[4], mutable=True)
querystring[REDIRECT_FIELD_NAME] = next_url
sudo_url_parts[4] = querystring.urlencode(safe="/")
return HttpResponseRedirect(urlunparse(sudo_url_parts))
示例11: redirect_to_login
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def redirect_to_login(next: str, login_url: Optional[str]=None,
redirect_field_name: str=REDIRECT_FIELD_NAME) -> HttpResponseRedirect:
"""
Redirects the user to the login page, passing the given 'next' page
"""
resolved_url = resolve_url(login_url or settings.LOGIN_URL)
login_url_parts = list(urllib.parse.urlparse(resolved_url))
if redirect_field_name:
querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next
# Don't add ?next=/, to keep our URLs clean
if next != '/':
login_url_parts[4] = querystring.urlencode(safe='/')
return HttpResponseRedirect(urllib.parse.urlunparse(login_url_parts))
# From Django 1.8
示例12: redirect_to_login
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Redirect the user to the login page, passing the given 'next' page.
"""
resolved_url = resolve_url(login_url or settings.LOGIN_URL)
login_url_parts = list(urlparse(resolved_url))
if redirect_field_name:
querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next
login_url_parts[4] = querystring.urlencode(safe='/')
return HttpResponseRedirect(urlunparse(login_url_parts))
# Class-based password reset views
# - PasswordResetView sends the mail
# - PasswordResetDoneView shows a success message for the above
# - PasswordResetConfirmView checks the link the user clicked and
# prompts for a new password
# - PasswordResetCompleteView shows a success message for the above
示例13: setup_wanted
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def setup_wanted(view_func, setup_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the user has completed the setup
process, redirecting to settings.SETUP_URL if required
"""
@wraps(view_func)
def wrap(request, *args, **kwargs):
if (
not request.user.is_authenticated
or request.user.account.has_completed_setup
):
return view_func(request, *args, **kwargs)
else:
resolved_setup_url = resolve_url(setup_url or settings.SETUP_URL)
path = request.get_full_path()
return redirect_to_login(path, resolved_setup_url, redirect_field_name)
return wrap
示例14: check_setup
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def check_setup(request):
"""
Checks if a user has completed setup
"""
if not request.user.is_authenticated or request.user.account.has_completed_setup:
return {"account_needs_setup": False, "current_user_account": None}
elif request.user.account.has_completed_setup:
return {
"account_needs_setup": False,
"current_user_account": request.user.account,
}
else:
resolved_setup_url = resolve_url(settings.SETUP_URL)
return {
"account_needs_setup": True,
"account_setup_url": resolved_setup_url,
"current_user_account": request.user.account,
}
示例15: test_resume_adding_talk
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import resolve_url [as 別名]
def test_resume_adding_talk(self):
user = User.objects.create(
username="testuser", password="12345", is_active=True
)
c = Client()
response = c.force_login(user)
response = c.get(resolve_url("add-talk"))
assert response.status_code == 302
assert response.url == resolve_url("add-speaker")
response = c.get(resolve_url("add-speaker"))
assert response.status_code == 200
response = c.post(
resolve_url("add-speaker"), {"title": "test", "bio": "testing"}
)
assert response.status_code == 302
assert response.url == resolve_url("add-talk")