Is there a way of grabbing a subset of the URL in Django views.py?
I have tried request.build_absolute_uri() but that captures slightly more than I want.
E.G.
If the URL was http://127.0.0.1:8000/jobs/new I would like to get http://127.0.0.1:8000/
Instead I end up getting the entire URL, with the page names.
I am wondering if there is a rootURL function in Django or something similar maybe?
This question has a few approaches which are:
Using request.get_host(),
Go back home
Using href='/',
Go back home
Or Using the root url defined in your url conf.
url(r'^mah_root/$', 'someapp.views.mah_view', name='mah_view'),
Then in your template:
Go back home
Related
I am trying to build an website which renders some books and the corresponding pages. I want to make possible to access a page like this:
path('/<str:book_pk>-<int:book_page>/', views.TestClass.as_view(), name='book-test')
I want a user to access it very simple, something like: mysite.com/5-12/ - which redirects him to the book nr 5 at page 12. The problem is that when I access this page from the website itself, using href, the real path becomes:
mysite.com/%2F5-13/
If I want to write in the browser, the following path: myste.com/5-13/, it throws me 404 page not found, because the real path is mysite.com/%2F5-13/ . This is pretty obvious, but my question is:
How can I stick to my initial path, and make it possible to be accessed via myste.com/5-13/? For some reason, Django URL Patterns, adds an extra %2F string at the beginning. Can somebody explain me why, and how to solve this issue?
I much appreciate your time and effort! Thank you so much!
You don't have to include / at the beginning of the url, simply:
path('<str:book_pk>-<int:book_page>/', views.TestClass.as_view(), name='book-test')
/ is encoded automatically as %2F in urls (read the full list here)
Basically, my goal is to redirect the user to the view that renders index.html if nothing from URLConf matched the requested url. Then, on the clientside, I would manage routing as appropriate or show 404 page.
So basically, the question is,
how to implement this redirecting in Django? I have tried this:
url(r'^(.*)$', views.index),
...some other urls
...and not surprisingly, (.*) means that ALL urls are redirected to index.html, no matter if there it matches any other URL or not. So should I just place this in the very end of the url list? I have a feeling that I should use some sort of middleware, or maybe Django has a native way to address this issue?
An umpleasant side-effect of this would be for users of my rest app. Each time they all an invalid URL, they would get HTML (that would in effect be the HTML for home.html) instead of json or 404 error. Well, nothing ugly about index.html code, and yet it sounds like a bad idea. So would I separate this logic so that the users of REST app get 404 error instead of being redirected to index.html? This is much about coding patterns, not Django itself. And yet I would really appreciate any comments on this as well :)
You can add the catch all at the end of your URL config, but it leads to some unexpected behaviour (e.g. redirects from /my-page to /my-page/ (appending slash) no longer work).
A better approach would be to write a custom middleware. You could look at the redirect middleware for inspiration since it handles redirects.
If you don't want to redirect API requests, you could add a check to your middleware. For example you could not redirect requests where the URL starts with /api/, or the request has an Accept: application/json header.
Another option would be to use a custom 404 handler that renders your index page. Again, in the handler you have the request object, so you can treat API requests differently.
I am currently going through various Django tutorials in order to understand how url mapping is working . I came across an example which is something like this
this is in my urls.py
url(r'admin_page_edit$',"adminApp.views.showClientDetails",name="admin_page_edit"),
this is in my html page that is currently being displayed to the user
<a href="{% url "admin_page_edit" %}?uname=SomeVal&par2=value" >
Now the URL the browser shows when the above href link is clicked. No problem there
http://127.0.0.1:8000/admin_page_edit?uname=SomeVal&par2=value
And the above URL lands in the corresponding view
adminApp.views.showClientDetails
Now here is the proble, this seems to all work but I am confused as to why this is working ? since the url of the browser is
http://127.0.0.1:8000/admin_page_edit?uname=SomeVal&par2=value
which does not match the regex string in the url
admin_page_edit$
(The above regex means if the string ends with admin_page_edit) but the url string does not end with admin_page_edit instead it is
http://127.0.0.1:8000/admin_page_edit?uname=SomeVal&par2=value
thus ending with par2=value
My question is why is this hitting the corresponding definition in the view when the url regex is not matching ?
Query strings (parts following ?) are not processed by the Django url parser. Why? Because they don't have to be processed. You can just about append any query string to any url:
Like: https://www.facebook.com/?request=pleasedonotwork which works all the same. Unless redirects (or some logging) are done based on queries sent in urls, you can consider the query part of urls as passive.
These query strings can be accessed in your Django views via the request.GET QueryDict
I'm trying to scrape a site that uses a ton of relative URLs. One archive page has links to many individual entries, but the URL is given like "../2011/category/example.html"
For each entry, I want to open the page and scrape it, but I'm not sure what the most efficient way to handle that is. I'm thinking of splitting the starting URL by "/", pop off the last item and re-joining them, to get the base URL.
That seems like such a cludge, though. Is there a cleaner way?
To construct an absolute URL from a relative URL, use urlparse.urljoin (docs here).
If you are using a browsing system like mechanize for crawling, however, you can simply fetch an absolute url initially and then feed the browser relative urls after that. The browser will keep track of state and fetch the URL from the same domain as the previous request automatically.
I need to know the full url for the current page from within a Mako template file in Pylons.
The url will be using in an iframe contained within the page so it needs to be known when the page is being generated rather than after the page hits the server or from the environment. (Not sure if I am communicating that last bit properly)
Not sure if this is the Pylons way of doing things but ${request.url} seems to work for me.
I think you can use h.url_for('', qualified=True) to get the full URL.
Make sure you have imported url_for in your helper file: from routes.util import helpers as h
Have a look at http://pylonshq.com/docs/en/0.9.7/thirdparty/routes/#routes.util.url_for