Does django support hashbang in its URL? - python

Does django support urls having #! in it.Say I have ursl like
http://example.com/foo/!#/bar
Any regex for that?
Note: http://example.com/foo/ and http://example.com/foo/#!/bar are different URLs and having different corresponding Views.
url(r'^(?P<#!>.+)/bar/$', 'my_view', name='my-view'), throwing bad character in group name

No. Anything after the # is not even sent to your webserver. If you want to make interactivity using the anchored (#) url styles, you need to go look at ajax libraries like jquery.

This is the "evil standard" way of denoting AJAX URLs. For a better description of the topic you should referer to this document http://code.google.com/web/ajaxcrawling/ which describes both what they are and how then to make URL handlers on your site handle them.

Related

Is it possible to use multiple slash Django URL as one variable in Django?

I'm new to Django. I'm now creating a project. In that project, I've links like this:
https://localhost:8000/example.com/example/path/
In the URL the example.com/example/path/ can be dynamically long as like this
example.com
or
example.com/asset/css/style.css
or
domain.com/core/content/auth/assets/js/vendor/jquery.js
I've used <str:domainurl> But is not working. As it has multiple forward slashes. And the forward slashes URL length generated while web scraping.
So is there is a way to use the full URL as one variable?
You want to be using the path path converter [Django docs]:
path('<path:domainurl>/', some_view)
Quoting Django docs:
path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than a segment of a URL path as with str.
Note: Design your url patterns and order them carefully if you are going to use this. Django uses the first matching url pattern to
serve any request.
If you want Django to match some custom strings take a look at re_path (https://docs.djangoproject.com/en/3.1/ref/urls/#django.urls.re_path) function. It basically allows you to pass a regular expression.
For the urls samples you provided you would probably want an expression like this (in your urls.py):
re_path(r'^(?P<my_url>[a-z0-9/\.]+)$', your_view_function)
This will pass a my_url kwarg to your view which you can then process as you want.

Strip http protocol from url before display in web2py?

When using IS_URL validator web2py prepends http to the stored value. Now when forming a link in my view I'd like to remove the http and trailing slash.
<div>{{=A('<what goes here?>', _href=business.website)}}</div>
That is, given a url such as:
http://www.example.com/
I want the anchor text to be
www.example.com (or example.com)
I understand I can do this via standard python using urlparse.urlsplit, however wondering if web2py provides this functionality?
If using pure python is the best way to do this, where should the code to strip the url be? View? Controller?
I understand I can do this via standard python using urlparse.urlsplit, however wondering if web2py provides this functionality?
No, web2py does not provide that functionality, so just use standard Python.
If using pure python is the best way to do this, where should the code to strip the url be? View? Controller?
Your choice. If you do it in the view, you can avoid having to pass an additional variable from the controller.
Note, you can do IS_URL(prepend_scheme=None) if you want to prevent the validator from prepending the "http" (you can also set prepend_scheme to an alternative, such as "https").

Django Url Dispatcher regex

I want SOMETHING to be non mandatory so basically I want www.site.com/endpoint to redirect to www.site.com/something/endpoint automatically. I there a way to do it on one line?
Right now I am doing:
url(r'^SOMETHING/endpoint$', 'endpoint', name='endpoint'),
url(r'^endpoint$', RedirectView.as_view(url='SOMETHING/endpoint')),
Cheers.
Why do you want to do this on one line? You're talking about two different URLs that do two different things—one is doing an HTTP redirect and the other is rendering a view. Two lines is the right way to go.
Writing a broader regex to cover both URLs will allow you to use the same view for both, but will not cause a redirect (that is, it will not change the URL to SOMETHING/endpoint).
I believe you are asking to let any url ending in "endpoint" to redirect.
To accomplish this change your redirect url regex to r"endpoint$". The caret operator in regex essentially says from the start of the string.
This regex will match for any url ending in "endpoint", eg. foo/endpoint, bar/endpoint

How can I write the regex for those urls

I'm trying to build a small wiki, but I'm having problems writing the regex rules for them.
What I'm trying to do is that every page should have an edit page of its own, and when I press submit on the edit page, it should redirect me to the wiki page.
I want to have the following urls in my application:
http://example.com/<page_name>
http://example.com/_edit/<page_name>
My URLConf has the following rules:
url(r'(_edit/?P<page_name>(?:[a-zA-Z0-9_-]+/?)*)', views.edit),
url(r'(?P<page_name>(^(?:_edit?)?:[a-zA-Z0-9_-]+/?)*)', views.page),
But they're not working for some reason.
How can I make this work?
It seems that one - or both - match the same things.
Following a more concise approach I'd really define the edit URL as:
http://example.com/<pagename>/edit
This is more clear and guessable in my humble opinion.
Then, remember that Django loops your url patterns, in the same order you defined them, and stops on the first one matching the incoming request. So the order they are defined with is really important.
Coming with the answer to your question:
^(?P<page_name>[\w]+)$ matches a request to any /PageName
Please always remember the starting caret and the final dollar signs, that are saying we expect the URL to start and stop respectively right before and after our regexp, otherwise any leading or trailing symbol/character would make the regexp match as well (while you likely want to show up a 404 in that case).
^_edit/(?P<page_name>[\w]+)$ matches the edit URL (or ^(?P<page_name>[\w]+)/edit$ if you like the user-friendly URL commonly referred to as REST urls, while RESTfullnes is a concept that has nothing to do with URL style).
Summarizing put the following in your urls:
url(r'^(?P<page_name>[\w]+)$', views.page)
url(r'^_edit/(?P<page_name>[\w]+)$', views.edit)
You can easily force URLs not to have some particular character by changing \w with a set defined by yourself.
To learn more about Django URL Dispatching read here.
Note: Regexp's are as powerful as dangerous, especially when coming on network. Keep it simple, and be sure to really understand what are you defining, otherwise your web application may be exposed to several security issues.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski
Please try the following URLs, that are simpler:
url(r'_edit/(?P<page_name>[\w-]+), views.edit)'
url(r'(?P<page_name>[\w-]+), views.page),

Getting a URL with Python

I'm trying to do something similar to placekitten.com, wherein a user can input two strings after the base URL and have those strings alter the output. I'm doing this in Python, and I cannot for the life of me figure out how to grab the URL. In PHP I can do it with query string and $_REQUEST. I can't find a similar method in Python that doesn't rely on CGI.
(I know I could do this with Django, but that's serious overkill for this project.)
This is just by looking at the docs but have you tried it?
cherrypy.request.path_info
The docs say:
The ‘relative path’ portion of the Request-URI. This is relative to the script_name (‘mount point’) of the application which is handling this request.
http://docs.cherrypy.org/stable/refman/_cprequest.html#cherrypy._cprequest.Request.path_info

Categories

Resources