I need to pass an object to redirect function. Is this the way to go about it ?
redirect('/view', kwargs={'obj': obj})
def view(request, obj):
do something
How do I implement it so url dispatcher accepts it ? I would use:
url(r'view/.+', view, name='whatever'),
Since I'm passing an object I'm not using url converters (str, int, slug, etc) but using regular expression that tells url dispatcher to accept anything I pass to it, however this is throwing me an error.
What is the right way to go about it ?
How do I implement it so url dispatcher accepts it?
You can not do that. you can not pass an object itself through a URL (or through anything else), or at least not without using a lot of "hacks", by using a serializer like pickle, but that would generate huge URLs (URLs are not strictly limited, but usually it is advisable to keep a URL shorter than 2000 characters). An empty dictionary will already take 8 characters if you encode it with a base64 encoding over a pickle stream.
You can not pass a generic object to a URL. You can only pass text as the URL. You can convert an integer to text, etc. But converting a generic object to text, that strictly speaking can be done (through pickling), but that is not recommended at all. It would furthermore potentially expose the webserver internals. If for example your object somehow refers to the Django settings, it might encode the database credentials, and thus impose a severe security risk.
You thus can pass text, or convert some data to text. In order to handle that, you first need to define a parameter in your url. How else will you capture the data. We can thus define a parameter, like:
url(r'view/(?P<obj>.*)/$', view, name='whatever'),
You can then generate a redirect by using the name of the view, and passing the parameters as named parameters:
redirect('whatever', obj='sometext')
Then the view will be called with obj as 'sometext'. Usually one passes the primary key, slug, etc. This makes it easy to read, compact, and furthermore it only exposes a small amount of data to the client.
Related
I've been reading lots of tutorials and stackoverflow questions, but I haven't found how to do this. I need to pass a parameter of type Date in a GET call on the URL.
I know it sounds easy, but I'm new to Django and what I have found is mostly something like this:
Django url that captures yyyy-mm-dd date
All those answers solve the issue if I want to have an URL like
http:www.myweb.com/2021/09/02
That is great if I want to write an url for a blog or website, but I'm doing it for an endpoint, and in my case, I need something like this
/some-action-endpoint/?created_at_gte=2020-01-01
So, I need to capture a parameter (hopefully named created_at_gte) with the value 2020-02-01 (It will awesome if I could capture that immediately as a Date object, but I'm fine with a string)
So, my questions are:
1.- It will be enough to create a group like this
url(r'^my-endpoint/(?P<created_at_gte>[(\d{2})[/.-](\d{2})[/.-](\d{4})]{4})/$', views.my_view),
Note_: by the way, the previous one is not working, if someone knows why it'll greatly appreciate it.
2.-The endpoint needs two parameters: created_at_gte and created_at_lte. How can I handle this? Do I need to add two url's to the url pattern?
I'm using Django 1.11 and Python 2.7. Cannot use other versions.
Query parameters aren't part of the URL matching behavior, so unfortunately your requirement to handle them as query parameters instead of URL path elements means you have to do more on your own rather than relying on pattern matching to reject bad requests.
You can do what you want as long as your base URL pattern (^my-endpoint/ in your example) is unambiguous and you're willing to handle the possibility that the parameters may not be set or may be set to something that doesn't parse as a date in your view, but you won't get the regexp-derived guarantees or the ability to bind them to view function parameters.
Instead, you'll have to extract them from the request.GET QueryDict object. That would look something like this:
In your url patterns:
url(r'^my-endpoint/$', views.my_view),
And then in your views:
def my_view(request):
created_at_start = request.GET.get("created_at_gte")
created_at_end = request.GET.get("created_at_lte")
# you now have no guarantees about these values except that if not None they'll be strings
# remember to do something appropriate for None, empty strings, strings that aren't dates, strings where end is before the start, etc
I have what I believe to be common but complicated problem to model. I've got a product configurator that has a series of buttons. Every time the user clicks on a button (corresponding to a change in the product configuration), the url will change, essentially creating a bookmarkable state to that configuration. The big caveat: I do not get to know what configuration options or values are until after app initialization.
I'm modeling this using EmberCLI. After much research, I don't think it's a wise idea to try to fold these directly into the path component, and I'm looking into using the new Ember query string additions. That should work for allowing bookmarkability, but I still have the problem of not knowing what those query parameters are until after initialization.
What I need is a way to allow my Ember app to query the server initially for a list of parameters it should accept. On the link above, the documentation uses the parameter 'filteredArticles' for a computed property. Within the associated function, they've hard-coded the value that the computed property should filter by. Is it a good idea to try to extend this somehow to be generalizable, with arguments? Can I even add query parameters on the fly? I was hoping for an assessment of the validity of this approach before I get stuck down the rabbit hole with it.
I dealt with a similar issue when generating a preview popup of a user's changes. The previewed model had a dynamic set of properties that could not be predetermined. The solution I came up with was to base64 encode a set of data and use that as the query param.
Your url would have something like this ?filter=ICLkvaDlpb0iLAogICJtc2dfa3
The query param is bound to a 2-way computed that takes in a base64 string and outputs a json obj,
JSON.parse(atob(serializedPreview));
as well as doing the reverse: take in a json obj and output a base64 string.
serializedPreview = btoa(JSON.stringify(filterParams));
You'll need some logic to prevent empty json objects from being serialized. In that case, you should just set the query param as null, and remove it from your url.
Using this pattern, you can store just about anything you want in your query params and still have the url as shareable. However, the downside is that your url's query params are obfuscated from your users, but I imagine that most users don't really read/edit query params by hand.
I am working on a Django setup where I can receive a url containining a query string as part of a GET. I would like to be able to process the data provided in the query string and return a page that is adjusted for that data but does not contain the query string in the URL.
Ordinarily I would just use reverse(), but I am not sure how to apply it in this case. Here are the details of the situation:
Example URL: .../test/123/?list_options=1&list_options=2&list_options=3
urls.py
urlpatterns = patterns('',
url(r'test/(P<testrun_id>\d+)/'), views.testrun, name='testrun')
)
views.py
def testrun(request, testrun_id):
if 'list_options' in request.GET.keys():
lopt = request.GET.getlist('list_options')
:
:
[process lopt list]
:
:
:
:
[other processing]
:
:
context = { ...stuff... }
return render(request, 'test_tracker/testview.html', context)
When the example URL is processed, Django will return the page I want but with the URL still containing the query string on the end. The standard way of stripping off the unwanted query string would be to return the testrun function with return HttpResponseRedirect(reverse('testrun', args=(testrun_id,))). However, if I do that here then I'm going to get an infinite loop through the testrun function. Furthermore, I am unsure if the list_options data that was on the original request will still be available after the redirect given that it has been removed from the URL.
How should I work around this? I can see that it might make sense to move the parsing of the list_options variable out into a separate function to avoid the infinite recursion, but I'm afraid that it will lose me the list_options data from the request if I do it that way. Is there a neat way of simultaneously lopping the query string off the end of the URL and returning the page I want in one place so I can avoid having separate things out into multiple functions?
EDIT: A little bit of extra background, since there have been a couple of "Why would you want to do this?" queries.
The website I'm designing is to report on the results of various tests of the software I'm working on. This particular page is for reporting on the results of a single test, and often I will link to it from a bigger list of tests.
The list_options array is a way of specifying the other tests in the list I have just come from. This allows me to populate a drop-down menu with other relevant tests to allow me to easily switch between them.
As such, I could easily end up passing in 15-20 different values and creating huge URLs, which I'd like to avoid. The page is designed to have a default set of other tests to fill in the menu in question if I don't suggest any others in the URL, so it's not a big deal if I remove the list_options. If the user wishes to come back to the page directly he won't care about the other tests in the list, so it's not a problem if that information is not available.
First a word of caution. This is probably not a good idea to do for various reasons:
Bookmarking. Imagine that .../link?q=bar&order=foo will filter some search results and also sort the results in particular order. If you will automatically strip out the querystring, then you will effectively disallow users to bookmark specific search queries.
Tests. Any time you add any automation, things can and will probably go wrong in ways you never imagined. It is always better to stick with simple yet effective approaches since they are widely used thus are less error-prone. Ill give an example for this below.
Maintenance. This is not a standard behavior model therefore this will make maintenance harder for future developers since first they will have to understand first what is going on.
If you still want to achieve this, one of the simplest methods is to use sessions. The idea is that when there is a querystring, you save its contents into a session and then you retrieve it later on when there is no querystring. For example:
def testrun(request, testrun_id):
# save the get data
if request.META['QUERY_STRING']:
request.session['testrun_get'] = request.GET
# the following will not have querystring hence no infinite loop
return HttpResponseRedirect(reverse('testrun', args=(testrun_id,)))
# there is no querystring so retreive it from session
# however someone could visit the url without the querystring
# without visiting the querystring version first hence
# you have to test for it
get_data = request.session.get('testrun_get', None)
if get_data:
if 'list_options' in get_data.keys():
...
else:
# do some default option
...
context = { ...stuff... }
return render(request, 'test_tracker/testview.html', context)
That should work however it can break rather easily and there is no way to easily fix it. This should illustrate the second bullet from above. For example, imagine a user wants to compare two search queries side-by-side. So he will try to visit .../link?q=bar&order=foo and `.../link?q=cat&order=dog in different tabs of the same browser. So far so good because each page will open correct results however as soon as the user will try to refresh the first opened tab, he will get results from the second tab since that is what is currently stored in the session and because browser will have a single session token for both tabs.
Even if you will find some other method to achieve what you want without using sessions, I imagine that you will encounter similar issues because HTTP is stateless hence you will have to store state on the server.
There is actually a way to do this without breaking much of the functionality - store state on client instead of server-side. So you will have a url without a querystring and then let javascript query some API for whatever you will need to display on that page. That however will force you to make some sort of API and use some javascript which does not exactly fall into the scope of your question. So it is possible to do cleanly however that will involve more than just using Django.
I have a route defined as follows:
(r'^edit/(\d+)/$', 'app.path.edit')
I want to use the reverse function as follows:
url = reverse('app.path.edit', args=('-id-',))
The generated url gets passed to a js function, and client side code will eventually replace '-id-' with the correct numeric id. This of course won't work, because the 'reverse' function won't match the route, because the url is defined as containing a numeric argument.
I can change the route to accept any type of argument as follows, but then I loose some specificity:
(r'^edit/(.+)/$', 'app.path.edit'
I could create a separate url for each item being displayed, but I'll be displaying many items in a list, so it seems like a waste of bandwidth to include the full url for each item.
Is there a better strategy to accomplish what I want to do?
You can rewrite regexp like this:
(r'^edit/(\d+|-id-)/$', 'app.path.edit')
but I generally prefer this:
(r'^edit/([^/]+)/$', 'app.path.edit') # you can still differ "edit/11/" and "edit/11/param/"
Usually you will anyway need to check entity for existent with get_object_or_404 shortcut or similar, so the only bad is that you have to be more accurate with incoming data as id can contain almost any characters.
In my opinion, and easier solution would be to keep the original url and then pass the value '0' instead of '-id-'. In the client side then you replace '/0/' with the correct id. I think this is better because it doesn't obscure the url routing, and you don't lose specificity.
Let's say I have a site where all urls are username specific.
For example /username1/points/list is a list of that user's points.
How do I grab the /username1/ portion of the url from all urls and add it as a kwarg for all views?
Alternatively, it would be great to grab the /username1/ portion and append that to the request as request.view_user.
You might consider attacking this with middlware. Specifically using process_request. This is called before the urlresolver is called and you can do pretty much anything to the request (request.path in this case) you want to. You might strip out the username and store it in the request object. Specifics depend (obviously) on the conditions under which you do/do not want to remove the first path component.
Updated for comment:
Whichever way you go about it, when you call reverse() you have to give it the additional context info -- it can't just automagically figure it out for itself. Django doesn't play any man-behind-the-curtains games -- everything is straight Python and there isn't any global state floating around just off stage. I think this is a Good Thing™.