Django: uses two url patterns/functions for one call - django

I have the following urlpatterns, in this order:
url(r'^([^//]*)/forum/topics/$', showThreadTopics),
url(r'^([^//]*)/$', redirectFrontPage)
I go to:
http://localhost:8000/xxx/forum/topics/
showThreadTopics is called and the page is correctly shown. However, I happened to have a breakpoint set in redirectFrontPage and saw that this function is also called. This happens every time and I can print statements from it. It has no effect on the final result and does not cause any apparent network activity when looking in Firebug. There is nothing special about showThreadTopics. It just gets some data and renders them. Actually, redirectFrontPage is called with any of my urls.
How is that even possible? How do I avoid it? I have that last urlpattern because if someone types
www.mysite.com/users_site_name
then I want to redirect it to
www.mysite.com/users_site_name/home

I would guess that a static reference on the page, to something like a CSS, JS or image file, is being intercepted by that URL, since it captures everything that isn't previously captured by anything else. It may even be the browser's automatic request of a favicon.

Related

Cannot specify path variable on a Flow / Send Request

I was playing with Postman Flows, and I was trying to learn by using the Trello API. All requests work on their own if executed manually. I've also debugged values using the terminal to understand where the problem lies. Before that, here's a summary of what I'm doing.
Get all boards for a given trello workspace
For each board, delete that board.
The complete flow looks like this:
I've checked that on the last block Send Request, the looped value of /variable/id outputs the proper board id. I've done this by checking with a terminal block and a string block. I started suspecting that this is caused by a failure of Postman to understand that the variable I'm trying to use is a path variable and not a query parameter. As such I tried to pass a static value to the Send Request and it 404'ed as well (tech aside: in theory for n ids it should give me one 200 and n-1 404s since the variable is static and the board would not be able to be deleted multiple times).
My suspicion comes from the fact that when configuring the block for this request:
You do not get prompted to add the board variable. I've tried to type it in anyway, and even use combinations like :board, with no avail. In fact like I said above, if I use these variables with static values, it still 404s.
ignore the parsing message on the right hand side...
As you can see, board doesn't show up. Did I end up hitting a bug, or is this user error? One thing I do not know how to do, but would help clarify that the issue is that a null value is being passed on to the DELETE would be to output the request itself. On a terminal block I can only see the response.
Thanks in advance.
UPDATE:
After checking the Postman console on the app, I've noticed that in fact the path variable being used is whatever is set on the collection request. It's like it takes the URL as a static field and disregards the path variables. Any thoughts?
Path variables won't be available in your Send Request. Instead, define your path variable with an environment/collection/global variable (i.e. {{board}}) in the value of the path variable. Then it will show up the relevant block of your flow.

django-compressor writing new files in collect_static/CACHE on every request

I've a django website set up using django-compressor + memcached.
Not sure when it started, but I'm finding new css and js files in .../collect_static/CACHE/css and .../collect_static/CACHE/js every minute, like output.2fde5b60eff0.css.
I use django.contrib.staticfiles.storage.ManifestStaticFilesStorage.
I have no clue if this is normal, or happening because of some misconfiguration. But in every few days, I need to clean the server because of this.
Any suggestions what is going on here?
Update: It seems to be happening because of template variables inside css and js code, as per this answer, but as I've a lot of such variables, I still don't know how to fix this.
Ok, so I found the underlying reason.
It is not actually the presence of template variables like {{context_data_var}} within compressed code.
It is the presence of any such variables the values of which change on each request.
I had two such instances:
Storage keys for the third party storage service (such as Google or Amazon)
csrf tokens used in various ajax requests
For 1. above, I simply moved such code outside compress.
For 2., the solution is slightly involved. I had to move away from using {{csrf_token}}. Django explains it in detail here. We need to use the csrftoken cookie instead of the variable {{csrf_token}}, and django sets this cookie if there is at least one {% csrf_token %} in the template. I had one luckily in my base template, so the cookie was already getting set for me. I also had the getCookie() function defined for all pages.
Thus, I was able to get rid of the issue explained in my question.

reverse_lazy() and URL Loading?

I'm trying to wrap my head around Django concepts, but I struggle with the URLResolver reverse_lazy(). As far as I know, I have to use reverse_lazy() when I want to reverse to an URL that has not been loaded. So when I create a CBV and state a success_url, I use reverse_lazy(). This means that the URL is not imported when the file executes.
This is confusing to me because I think the server loads all the URLs while starting before executing anything. So how come the URL is not loaded in time of execution?
I would be very happy if someone would give me an answer to this.
Well, first of all, the URL resolver itself is lazy, so loading happens when the first call to resolve() or reverse() is made (usually on the first request). But that doesn't matter much in this case.
When the URL resolver is being initialised, it imports your URL configuration, which in turn imports your views. So at the time your view is imported and success_url is set, the resolver is only halfway through its initialisation. Calling reverse() at this point would not work since the resolver doesn't have all the information yet to reverse the view name.

onSessionEnd apparently not firing

I have an INSERT INTO Logfile in onSessionStart, and an UPDATE in onSessionEnd.
I've test it manually by calling onSessionEnd from elsewhere within the Application.cfc, so I know that it is working.
But that's the only time it's ever fired.
I wonder if onSessionEnd is never being fired.
I got enough upvotes on my comment, I figured I'd make it an answer for how to debug onSessionEnd methods.
First off, you need to remember that if you call onSessionEnd directly, it's being called during a regular request context. This means it has access to variables that it won't normally have access to if it's being called during the regular session end. This means that testing "manually by calling onSessionEnd" isn't a valid way to test the method.
To that end, the only way to reliably debug an onSessionEnd method is with judicious use of the cflog tag. You need to add cflog entries to flag when the method runs, you need to have error catching, to log errors or dump out cfcatch scopes to a file for review. You also need to make sure that anything you're referencing in the method is passed in via the SessionScope and ApplicationScope arguments, and that you're not referencing any scopes other than Arguments and Server. See livedocs for reference.
Hopefully that helps you find the source of your issue.
I'd add that you even can't call other Application.cfc functions from within onSessionEnd method.

creating a drupal non-cached template variable

In drupal 6 I'm trying to execute a function on every page and output a different link based on what IP address someone is coming from. However, when I try this, it seems that the result is getting cached. I have tried this as a module and in template.php, but have not gotten results. What is the best approach to make sure this function executes on every page load? Or is there an easy way to create a template variable that does not get cached?
Is it possible for you to use a block instead with BLOCK_NO_CACHE or BLOCK_CACHE_PER_USER as the block caching policy? If you put a block like this in a region above/below you could achieve a very similar effect on any page you like, node or otherwise.