Django object change doesn't show op on page - django

I've got an object, Question, that I've created an edit page for. This worked fine. Someone would edit the question and changes would show up on the page that just showed the question. Recently however, I started noticing the changes did NOT show up on the list of questions. This problem persists after having changed the cache-backend to the dummybackend. When running the developmentserver, I see the request with a nice 200 code. Print statements I put into the view, that I expected to show up in the output of de dev-server, do not show up. So apperently the view method isn't even called. I get the feeling the 200 code does not mean something wasn't retrieved from cache.
Three ways I noticed, make the website show the change in the object after it has been saved:
1. Signing the current user out of the website and then logging in again.
2. Appending ?something=whatever to the url.
3. Waiting for an unknown amount of time. I tried if the waitingtime could be changed by modifying session-parameters, but to no avail.
Though I think it is possible to use that last method, it doesn't feel right. And it means means quite a bit of work to solve a problem that wasn't there before. And I'd like to know just what happend.
Here is the cache-bit from settings.py. No surprises there I'd think:
CACHES = {
'default':{
'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}
And, because logging out & in helps, the session stuff:
SESSION_ENGINE = 'django.contrib.sessions.backends.file'
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_AGE = 60
Oh and this problem is also in the admin..
Suggestions?

Related

Django Bad Reqsut 400 issue

Had this Django Project that constant had this 400 issue but it miraculously solves itself.
6 months went and some setting change within the server and now the bad request had come back to haunt us.
I look up in this issue but every advice keeps saying the issue in 2 areas:
Within the Django setting.py first DEBGU = Ture second ALLOW_HOST = [empty]
I constantly relook over this fact to point I can even remember it in my sleep.
I would bag anyone who had knowledge of network, please share some possible solution.
Assuming it's actually spelled that way, you should change
DEBGU = Ture
to
DEBUG = True
Also, you can either set your ALLOWED_HOSTS to the host of your webserver/ip address, or just change it to:
ALLOWED_HOSTS= ['*']
albeit its not recommended, and I would set it your IP address of whatever server you're running it from (if you're running it locally use "127.0.0.1". Please make sure its ALLOWED_HOSTS with an S and the ED, not just ALLOW_HOST. Proper spelling is important, otherwise your code will throw many errors!

i need to restart django server to make my app properly work

so i made a python script to grab images from a subreddit (from Imgur and imgur albums). i successfully done that (it returns img urls) and wanted to integrate it into django so i can deploy it online and let other people use it. when i started running the server at my machine, the images from subreddit loads flawlessly, but when i try another subreddit, it craps out on me (i'll post the exception at the end of the post). so i restart the django server, and same thing happen. the images loads without a hitch. but the second time i do it, it craps out on me. what gives?
Exception Type: siteError, which pretty much encompasses urllib2.HTTPError, urllib2.URLError, socket.error, socket.sslerror
since i'm a noob in all of this, i'm not sure what's going on. so anyone care to help me?
note: l also host the app on pythoneverywhere.com. same result.
Using a global in your get_subreddit function looks wrong to me.
reddit_url = 'http://reddit.com/r/'
def get_subreddit(name):
global reddit_url
reddit_url += name
Every time, you run that function, you append the value of name to a global reddit_url.
It starts as http://reddit.com/r/
run get_subreddit("python") and it changes to http://reddit.com/r/python
run get_subreddit("python") again, and it changes to http://reddit.com/r/pythonpython
at this point, the url is invalid, and you have to restart your server.
You probably want to change get_subreddit so that it returns a url, and fetch this url in your function.
def get_subreddit(name):
return "http://reddit.com/r/" + name
# in your view
url = get_subreddit("python")
# now fetch url
There are probably other mistakes in your code as well. You can't really expect somebody on stack overflow to fix all the problems for you on a project of this size. The best thing you can do is learn some techniques for debugging your code yourself.
Look at the full traceback, not just the final SiteError. See what line of your code the problem is occurring in.
Add some logging or print statement, and try and work out why the SiteError is occurring.
Are you trying to download the url that you think you are (as I explained above, I don't think you are, because of problems with your get_subreddit method).
Finally, I recommend you make sure that the site works on your dev machine before you move on to deploying it on python anywhere. Deploying can cause lots of headaches all by itself, so it's good to start with an app that's working before you start.
Good luck :)

Django: Message is not receieving

I am using Django Postman for the intercommunication between two user of my django website .
But when i am sending a message to another user .It is not delivered to the recepient .
I shows me as sent message .In http://127.0.0.1:8000/messages/sent
Here is settings.py setting
############################# Django postman
POSTMAN_AUTO_MODERATE_AS = True
POSTMAN_SHOW_USER_AS = True
POSTMAN_NOTIFIER_APP = True
###################################
And once the messages is sent it is storing properly in the Dtabase but it is not appearing to the recepient inbox.
Please tell me what might I am doing wrong here .
I encountered seemingly identical problem, in my case cause was default moderation.
To better diagnose if this is the case, you can:
check your 'invisible' messages in database, for example using phpmyadmin. If there's 'p' as m moderation_status or anything suspicious under moderation_... columns, this track is probably good;
dig into code: locate postman/models.py and experiment with class MessageManager, method inbox (since other message directories are fine, this one is suspicious). Any of filters there might be cause of your problem - for me it was obviously 'moderation_status'. Even if your case is different, this is good starting point for further debugging.
Use case: let's assume moderation issues
I see that you have
POSTMAN_AUTO_MODERATE_AS = True
set, but perhaps you have left moderation function somewhere, or something gets overwritten in your configuration? Postman's Quick Start Guide indicates that both are necessary:
To disable the moderation feature (no control, no filter):
Set this option to True
Do not provide any auto-moderation functions
I'd suggest removing all other postman specific options from your settings.py, leaving only POSTMAN_AUTO_MODERATE_AS = True and check if there are any utility functions that could potentially interfere with Message objects.
For further reference, more information about moderation is here: https://bitbucket.org/psam/django-postman/wiki/Moderation

django - settings.py seems to load multiple times?

EDIT I miscounted - it is printed out twice, not four times.
I put this in my settings.py
print 'ola!'
and on startup "ola" is printed out twice! This seems like something is wrong with my pycharm django project ... any ideas why this would happen? It isn't in a loop or anything (that i'm aware of, anyway)
cheers!
YAY The user known only as "rohit", as per the comments, has determined that a solution can be found here: https://stackoverflow.com/a/2110584/1061426 ~ see the comment about disabling reloading.
CAUTION I don't have my Django code up and about now, so I don't know what the noload will do. Good luck, soldiers.
If you print out the thread ID within settings.py, you will see that settings.py is actually being loaded on two different threads.
See this stackoverflow response and this article for more info.
Actually what Django does is putting a wrapper around settings. It is basically an object (settings object if you want so) which give you access to some direct setters like settings.WHATEVER, so it appears like you access the global variables in settings.py direclty.
I really don't remember though, why the import happens twice. I looked into it once when I worked on django-dynamic-settings which uses a very similar approach as Django itself.
Anyway, if you are interested in the "magic" you can follow the flow starting from the execute_from_command_line call in manage.py.
Django does some strange things with settings.py, and it will execute more than once. I'm used to seeing it imported twice, not sure why in PyCharm you're getting four times. You have to be careful with statements with side-effects in settings.py.
A closely-related question has been asked at least twice since. I can add that a Django core developer rejected the idea that this is any sort of Django bug; it's normal behavior.
Also see this from Graham Dumpleton.

Django caching bug .. even if caching is disabled

I have a Django site where a strange bug is occurring.
On the site they can add "publications", which is basically the same thing as a blog post under a different name.
Things gets weird when they modify an existing post. They first modify it in the admin and when they go on the site, the change isn't visible. Like if the old version was cached.
In fact, at the beginning I was pretty sure it was a browser caching bug. But after some trials, things got a little weirder.
I found out that clearing browser cache or using a different browser does not solve the problem, but rather interestingly it toggles between the old version and the modified version upon refresh.
So if the body of the post was "Hello World" and I modify it to be "Goodbye cruel world" and then go to the site and refresh the page multiple times, I would see "Hello World", then "Goodbye cruel world", then "Hello World" and so on.. no matter how long I keep doing it.
But it doesn't stop there .. after about 24h everything falls back into place and work normally. No permutation anymore, the site sticks to the new version...
I'm pretty much speechless because I built well over 50 other Django sites using the same server and I never had this problem before.
I'm using the latest django (1.3) with a MySQL DB and caching is not enabled..
Any ideas ?
Edit: A graceful restart of Apache solve the problem .. but restarting apache after each update isn't the greatest thing..
Update: I've just re-setuped my dev environement and I found out the bug is far more acute with the dev server. The modified contend won't show up until I kill/restart the dev server, no matter how often I refresh or clear my cache..
The problem is explicitly addressed in the generic views documentation. The querysets in your extra_context dictionary are evaluated once, when the urlconf is first processed, and each time after that they will continue to use the same values. That's why they only change when you reset Apache or the dev server.
The solution, as described on the linked page, is to use callables which return the querysets, rather than specifying the querysets in the dictionary itself.
I had a similar problem once. It turned out I created the object at the top of the urls.py, and the object was alive as long as the process was alive. You may be using a global variable in one of your views.
There are a few other ways to control cache parameters. For example, HTTP allows applications to do the following:
Define the maximum time a page should be cached.Specify whether a cache should always check for newer versions, only delivering the cached content when there are no changes. (Some caches might deliver cached content even if the server page changed, simply because the cache copy isn't yet expired.**)
In Django, use the cache_control view decorator to specify these cache parameters. In this example, cache_control tells caches to revalidate the cache on every access and to store cached versions for, at most, 3,600 seconds:
from django.views.decorators.cache import cache_control
#cache_control(must_revalidate=True, max_age=3600)
def my_view(request):
# ...
Any valid Cache-Control HTTP directive is valid in cache_control(). Here's a full list:
public=True
private=True
no_cache=True
no_transform=True
must_revalidate=True
proxy_revalidate=True
max_age=num_seconds
s_maxage=num_seconds