My current project’s getting a bit large, so I figured I should start subdividing the code a bit. The project’s already split up into multiple applications, but my main application is growing…
The first thing I’ve done is split up my models.py into multiple files and moved them into a folder named models.
Folder structure is: project/application/models/model_file.py
All that’s left to be done is to add an _init_.py to that folder and import all your models within the __init__.py. You’ll also need to make sure the app_label set on each of your models.
For example:
class Shape(models.Model):
value = models.CharField ()
class Meta:
app_label = "my_project"
This may turn into a series…lets see if there’s anything else that needs better organization.
Not sure how other distros have this setup, but I know this works with Ubuntu…
First off, to view your PYTHONPATH. Load the python shell, by just running “python” from your favorite prompt. Then the following.
>>> import sys
>>> for line in sys.path: print line
Modifying it is as simple as adding a path file (such as “myproject.pth”) to this folder:
/usr/local/lib/pythonx.y/site-packages
Then within the file “myproject.pth” put the path to the folder of interest.
Important asside:
Although this is useful to know, the reason I ended up figuring this out was to add the path of my project so that my own project could access a certain folder within the project. What I really missed was just adding an __init__.py within the folder I added to my project, which is why I couldn’t treat the folder as a module… Arg!
When completing something like deleting an item or adding a user, it’s usually nice to report a message to the user to let them know what just happened. I also find it useful to send the user somewhere they’d “want” to go. For example, if they just created a user, send them to the list of users page, so they see the change they just made.
I’m also a big advocate of keeping URLs clean.
Because of these concerns the best answer in Django is to do an HttpResponseRedirect. Now the problem with that is that you can’t pass data to a redirect. To get around this there are messages that you can send to the users session itself.
The mechanism isn’t that clean, but it does work, and it does do the job.
So if you have a buycbdproducts in user all you need to do to add messages is:
request.user.message_set.create(message='User successfully created.')
Now the flip side to the equation is that you “get” the message(s). So how this works is when you set a message you’re actually adding a message to a list. When you”get” the message, you’re actually getting the list (may be more than one message). And if you don’t “get” the message at the right time, the messages will just build up. But for the most part this system works just fine. To get the messages you just do:
messages = request.user.get_and_delete_messages()
Then just pass ‘messages’ to the view and show the list to the user.
Every once in a while I end up starting a project, and since it happens so infrequently I always forget the steps to set things up in subversion.
I won’t go through all the steps you need to start from scratch with subversion, but here’s the important part about adding a project to your repo.
svn import <path to project> file:///<path to svn root>/<project folder> -m "Initial import"
svn import /home/PROJ file:///home/svn/PROJ -m "Initial import"
import: This adds a directory tree to your
path to project: Actual path to directory tree where code sits
path to svn root: Path to repo
project folder: Name for project folder in svn
The one thing I’m not sure how to do right now is after I import the project. The folder with the code is not a subversion checkout. So what I usually end up doing is checking out the project and replacing the checked out version over my original code. Not sure if there’s a better way to do that…