Migrating a django app from 1.3.1 to 1.5 - django

We are considering migrating our dated django 1.3.1 app to the latest version (1.5.4).
I don't like migrations.
Are any specific problems to be thought of? Any show stoppers you may think of?
Of course I will study release notes and all this.
Will it be a real head ache or is it doable?

We have migrated our app from 1.4 to 1.5, we didn't fetch any issue which is blocker. We had needed to done couple of changes related to adminmedia, json library uses and url syntaxing in our html templates. They were some normal issues only.
But yes as you said its always better to check release notes before upgrading to latest version.
Hope this will help you.

Here are the few things you need to take care of:
Project directory structure has been changed.
Include ALLOWED_HOSTS in settings.py.
django.utils.simplejson is deprecated. import simplejson can be used instead.
Session data will not be saved, when the response code is 500.
django.forms.ModelMultipleChoiceField now returns an empty QuerySet as the empty value instead of an empty list.
Uploaded files are no longer created as executable by default. If you need them to be executable change FILE_UPLOAD_PERMISSIONS to your needs.
{% load adminmedia %} template tag can no longer be used.
If you’re using django.contrib.redirects, make sure INSTALLED_APPS contains django.contrib.sites.

Related

Django 1.7 tutorial: Use generic views

I need to use Jython instead of Python, I found that jython2.7b2 works with DJango 1.7. So, I am stuck using the beta version. I am trying to follow the current Django tutorial and I have ran into a problem. I am not sure if I am using generic views properly. When I try to change the urls.py (polls) file. I see that pydev complains that views.IndexView, views.DetailView, and ResultsView don't exist. Am I doing something wrong? Or did they change the way generics work in version 1.7?
My System:
Windows 7
jython2.7b2
Django-1.7c3
postgresql-9.3.5-1-windows-x64
postgresql-9.3-1102.jdbc41.jar
Here is the url for the tutorial, go to the section "Use generic views: Less code is better"
Django tutorial part 04
Once you have changed your urls.py, you need to define the new views.
This is covered in the next section of the tutorials, Amend views.

Django doesn't read from database – no error

I just set up the environment for an existing Django project, on a new Mac. I know for certain there is nothing wrong with the code itself (just cloned the repo), but for some reason, Django can't seem to retrieve data from the database.
I know the correct tables and data is in the db.
I know the codebase is as it should be.
I can make queries using the Django shell.
Django doesn't throw any errors despite the data missing on the web page.
I realize that it's hard to debug this without further information, but I would really appreciate a finger pointing me to the right direction. I can't seem to find any useful logs.
EDIT:
I just realized the problem lies elsewhere. Unfortunately I can't delete this post with the bounty still open.
Without seeing any code, I can only suggest some general advice that might help you debug your problem. Please add a link to your repository if you can or some snippets of your database settings, the view which includes the database queries etc...
Debugging the view
The first thing I would recommend is using the python debugger inside the view which queries the database. If you've not used pdb before, it's a life saver which allows you to set breakpoints in your Python script and then interactively execute code inside the interpreter
>>> import pdb
>>> pdb.set_trace()
>>> # look at the results of your queries
If you are using the Django ORM, the QuerySet returned from the query should have all the data you expect.
If it doesn't then you need to look into your database configuration in settings.py.
If it does, then you must might not be returning that object to the template? Unlikely as you said the code was the same, but double check the objects you pass with your HttpResponse object.
Debugging the database settings
If you can query the database using the project settings inside settings.py from the django shell it sounds unlikley that there is a problem with this - but like everything double check.
You said that you've set up a new project on a mac. What is on a different operating system before? Maybe there is a problem with the paths now - to make your project platform independent remember to use the os.path.join() method when working with file paths.
And what about the username and password details....
Debugging the template
Maybe your template is referencing the wrong object variable name or object attribute.You mentioned that
Django doesn't throw any errors despite the data missing on the web
page.
This doesn't really tell us much - to quote the Django docs -
If you use a variable that doesn’t exist, the template system will
insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is
set to '' (the empty string) by default.
So to check all the variables available to your template, you could use the debug template tag
{{ debug }}
Probably even better though is to use the django-debugging-toolbar - this will also let you examine the SQL queries your view is making.
Missing Modules
I would expect this to raise an exception if this were the problem, but have you checked that you have the psycopg module on your new machine?

How to use Django 1.4 with Xeround?

According to Xeround's Release Notes they don't support save points and I can't figure out how to turn off support for this in Django 1.4. Does anyone know how to accomplish this?
I had the same problem. Django seems to only check the version of the MySql when it decides whether to use savepoints or not. Xeround probably uses some non-standard database-engine that doesn't support savepoints even if the MySql version is high enough.
Quick fix (just for testing) is to just edit the django/db/backends/mysql/base.py to override the logic:
Before:
self.features.uses_savepoints = self.get_server_version() >= (5, 0, 3)
After:
self.features.uses_savepoints = False
I tested this and it didn't seem to cause problems.
Note: Editing django sources directly like this is not recommended, you probably should just create your own db backend module by subclassing or copying the mysql module and placing it inside your project. Remember to update settings.py database configuration to point to your module.

Django Static File Issue on Deployment

Hello i'm trying to deploy a django app which uses the {{ STATIC_URL }} tag.
When I put my code up on EC2 (in debug mode) I get the following error:
Module "django.core.context_processors" does not define a "static" callable
request processor
This doesn't happen on ./manage.py runserver
This also disapears when I remove the django.core.context_processors.static from my template context processors (but then I don't get static media)
Anyone know whats going on?
The static context_processor - and the staticfiles app - were added in version 1.3. It looks like you're running an older version in production.
See also: http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/
I hope you found a solution. But since no one decided to put one here I will, In the event any one new to Django such as myself stumble upon this error, here is my solution. If you carefully read the error assuming you have DEBUG=True you will come to realise that the static definition(pythonic function) is missing from context_processors.py in the django.core module. Therefore head there in your directory. on my server it was /usr/lib/python2.6/dist-packages/django/core. Edit the context_processors.py and add the following function
def static(request):
# this func will static-related context variables to the context
return {'STATIC_URL': settings.STATIC_URL}

Storing important singular values in Django

So I'm working on a website where there are a couple important values that get used in various places throughout the site. For example, certain important dates, like the start and end dates for registration.
One way I can do this is making a model that stores these values, but that sounds like overkill (since I'd only have one instance). Another way is to store these values in the settings.py file, but if I wanted to change them, it seems like I would need to restart the webserver for them to take effect. I was wondering what would be the best practice in Django to handle this kind of stuff.
You can store them in settings.py. While there is nothing wrong with this (you can even organize your settings into multiple different files, if you have to many custom settings), you're right that you cannot change these at runtime.
We were solving the same problem where I work and came up with a simple app called django-constance (you can get it from github at https://github.com/comoga/django-constance). What this lets is store your settings in a settings.py, but once you need to turn them into settings configurable at runtime, you can switch to a Redis data store with django admin frontend. You can even use the value from settings as your default. I suggest you try this app out.
The changes to your code are pretty minimal, as pasted from docs you initialize your dynamic settings like this:
CONSTANCE_CONFIG = {
'MY_SETTINGS_KEY': (42, 'the answer to everything'),
}
And then instead of importing settings from django conf, you do this:
from constance import config
if config.MY_SETTINGS_KEY == 42:
answer_the_question()
If you want a specific set of variables available to all of your template, what you are looking for is Context Processors.
http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
More links
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/
The code for your context processors, can live anywhere in your project. You just have to add it to your settings.py under:
TEMPLATE_CONTEXT_PROCESSORS =
You could keep the define your constants in your settings.py or even under a constants.py and just
from constants import *
However as you mentioned, you would need to reload your server each time the settings are updated. I think you first need to figure out how often will you be changing these settings? Is it worth the extra effort to be able to reload the settings automatically?
If you wanted to automatically enable the settings, each time they are updated you could do the following:
Store settings in the DB
Upon save/change, write output to a file
settings.py / constants.py reads files
reload server
In addition, you have a look at the mezzanine project which allows you to update settings from the django admin interface and will reload as well.
See: http://mezzanine.jupo.org/docs/configuration.html
If the variables you need will be updated infrequently, i suggest just store them in settings.py and add a custom context processor.
If you are using source control such as GIT, updating will be quite easy, you can just update the file and push to your server. For really simple reloading of the server you could also create a post-recieve hook for git that will automatically reload the server when new code is pushed.
I would only suggest the other option if you are updating settings fairly regularly.