What is the right way to create projects in Django ? (Folders) - django

Sorry for the weird question.
I am creating a MMORPG Text browser game, and I feel like my project is wrong.
I am creating a new app for everything I need (Account app, Menu app, Fight app). And when I browse Github, people only have like 5/6 folders while I'm at 20 currently.
Where should I put my classes/forms/things normally ? Like, where should I have my fight system placed ?
Thanks ! (Sorry for bad english)

I think, it's completely up to you how you structure your project. But the common thinking is to make Django app reusable (check this out) and separate concerns.
For instance, when creating a Django project I follow these steps:
For every new feature, start a new app, which has its own models, views, templates, static content etc.
Create a separate app called core, to store some general functionality and static files.
Create a test folder instead of a single test.py. This folder has separate modules test_views.py, test_models, test_forms.py.
Create a settings folder which contains base settings general to all environments (i.e. development, testing, production) and then specific settings file for every environment.
I noticed, that this basic structure helps to maintain the order and decouples logic even though the project sometimes contains 15+ apps. If you configure your settings.py properly (e.g. add new apps to INSTALLED_APPS) Django will be able to put your project together nicely, hence I would say don't worry about the number of folders but look at the reusability and separation of concerns.
Also, it is difficult to say where your fight system should reside without seeing project architecture but I would guess that it will be split among these different apps and could be connected in the core app.

Related

Django - accessing a project prom another project

I've built a little project in Django- a rol web, with a user system and etc.
Now I want to host it under my personal web, which is another Django project with info about myself.
Is there any way to "import" the rol project in order to access it with a button from my personal web?
If that's so, how efficient would it be? Could I add a "personal projects"-portfolio like tabs with urls leading to different projects, all stored under then same project?
Thanks
First thing first: no, you cannot "import" a django project as part of another django project - not without a lot of pain, dirty hacks and unexpected breakages at least. Django has not been designed to be used that way.
You can of course reuse apps (assuming they've been developped with reusability in mind) from one project into another one, but you'll still have to (re)do all the project-level integration.
Another solution is to deploy those "side projects" under subdomains of your main site, ie if your main site is 'xbro.com', you could deploy your 'rol' project under rol.xbro.com (just plain ordinary django project deployment using vhosts), and add links between the two projects where you see fit (hard-coded in your base templates, managed by some specific "menu" or "mini-cms" app, etc). You can still share the database (or part of it) for user accounts or other common resources if needed (you might have to properly configure session cookie so they're shared between projects though).

Moving from PHP/Laravel to Python/Django

I want some clarity. I want to learn more about django and use it as replacement for php/laravel. But the default structure and convention of django confuses me a bit.
My PHP/Laravel project has 3 parts:
- Administration
- Core (Web app for regular users)
- API Service (REST-API for mobile apps)
However all of controllers, models and views are contained in a single Laravel application. I separated Auth, Admin, Api controllers into their own folders/namespaces.
One thing that confuses me is the default Django structure 1 view 1 model file. How should i go about reworking this application in Django should each of my controllers be a separate app in my django project or should I have same approach as in Laravel. 3 Django apps in one project one for admin one for core and one for api ? Where should I keep my models than since in Laravel all models are used by all 3 parts ?
My current structure:
./
./controllers/
./auth/
LoginController.php
RegistrationController.php
...
./admin/
ReportsController.php
UserController.php (Admins overview of all users)
...
./api/
HealthController.php (API CRUD for Health resource)
ExerciseController.php
HomeController.php
UserController.php (Regular users profile page CRUD)
...
./models/
User.php
Health.php
Exercise.php
...
One thing to remember about Django is that an app in Laravel doens't necessary translate to an app in Django. In Django, there are projects, and each project can have any number of apps. For example, I have a "Backup Admin" project where I manage a lot of the day-to-day issues of managing a tape backup environment. I have an app for media (that has 3 models, one for regular media, one for cleaning media, and one for media that we want to exclude from tape ejections). I have an app that represents the backup images, and another for backup jobs (to check status codes). Each sub-piece of my project goes into another app.
If I wanted to do another Django project that had nothing to do with backups, I'd make that a completely separate project, which would have a separate directory structure from my backup project. It'd have it's own urls.py, settings.py, etc.
Regarding the models piece, I put all of one app's models in the same file. For example, in my media app, I have models.py, which contains all three models that I mentioned above. This is completely optional, but I do it just so while importing these models into other parts of the project, I don't have to remember what the file names are, instead I can just do this:
from media.models import CleaningMedia,Media,EjectExclusions
Otherwise I'd have to have 3 different import statements if they were in different files. It's completely possible, but based on your preferences.
Regarding the controller, Django lets you do it either way. You have a project-wide urls.py file that you can use to control all of the traffic, or you can have separate urls.py files in each app to control that app's traffic. I prefer a single file, but that's just me. Personally if you have a lot of controller entries, you should probably split them up into app-specific urls.py files, just to keep it clean, but again, either method would work. I think of maintainability (especially with respect to teammates having to support it) when I make these types of decisions.
The admin interface is built-in, so there's not really an app for that, but you can decide which models and which apps have entries on the admin interface quite easily. Each app has an admin.py file that controls this.
A side note, for a RESTful API, you also might want to consider Django Rest Framework. It's a great piece of software, and the documentation (and tutorials) are very helpful.
Edit:
The 1 view/1 model thing again is just preference. You can have as many files as you want. The only trade off is when you import them into other files, you have to specify the file you're importing it from. That's really all there is to it. I know people who have a views/ directory, and inside there, have separate files for each view, keeping each class/function separate. Totally a matter of preference.

How to structure a project directory in django?

Suppose you are building a Google website. (ok big dream)
Google has web search/youtube/email/news/etc ..
For this site, I'd like to structure my django directory like
Google/
search
youtube
email
news
and so on.
How do I structure such a site?
Create an app for each even though I'm not expecting to publish any of the category as an app?
Where would a common stuff (such as user model, utility modules, decorators..) would go, create a common_app?
Applications are reusable components for a django project that revolve around a central purpose. Applications don't need to map directly to your url structure of the website. While there is a standard structure for a django application to tie in with some of the management commands, such as tests.py, models.py, static files at /static/ you don't need to have any of it to be an application. For example, South is a popular django application used to provide database migrations. It adds a few management commands to manage.py.
When you are adding functionality and it doesn't map directly to the purpose of the application, just create a new one. So instead of thinking of it a a common_app, think about what the purpose of the application would be and how it might be utilized by your other applications.
In my projects, I tend to create a base application to handle the base template and static assets that are used in the base template. I'll create an accounts application to handle the user model and implement things like password reset. To deal with global notifications from any part of my site, I'll create an alerts application. The list can go on for a lot of the common functionality, but it's grouped in a way that revolves around a function and written as if it would be distributed.
So, in your specific case, you'll likely have at least an application for each of the domains such as search, youtube, email, and news, but also an application for each common component you might want to use across your core domains.

How to setup groups (sub-sites) in Django

I'm new to Django and I come from Drupal family. There we have Organic Groups with which we can create groups of content and subsites; how do I do something like that with Django?
Say I'm making this site for my company using Django and every department in my company needs a private section on the site. For example, the design people have their own part of the website into which the back-end developers can not come in. And the back-end developers will have the same thing too.
I want to build the site in such a way that I just login into Django admin and add a new category or subsite or group (whatever the Django term is) with the same settings from other groups or with similar settings.
It depends on what you mean by "private section". You should probably try looking at it from a different angle:
Django splits a site's functionality by means of "apps". Each app does its specific thing, and gets a set of tables in the database. Apps can access each others' tables. For example, it's common for other apps to access the Auth app's user, group, and permissions tables. Is this what you mean by "sub sites"?
As for access control, users can be assigned to groups and they can have various administrative permissions assigned to them. Add, change, and delete permissions are automatically generated for each model (i.e. database table). You can also add your own permissions.
I don't think you'll be able to separate the designers from the back-end developers at the Django level. You'll need to do something else, such as maintain separate source repositories for each and merge them to create the usable site (each group would have read-only access to the other). It really depends on your teams' discipline, because these elements can get intertwined.
Django recommends that static files be served by something else, say directly from your web server, or from another machine with a simple HTTP server (no CGI/WSGI/whatever). This is because Django can only slow down static files compared to direct service. However, for testing, ther is a static page server you can enable.
Given all that, static files usually amount to CSS, images, media, and JavaScript. Of these, the back-end people might want to mess with the JS, but that's it, so this could be in the designers' repo.
The Django tree itself has the code for the site and the apps. It's almost all back end stuff. The exception is the HTML template files, located in the "templates" directory in each app. These are the files that are filled in with the context data supplied by the back-end view code. I have no idea if this is front or back end for you guys; it could be mostly back end if there's a lot of CSS discipline, but I think that's unlikely.
There are a lot of things that you can do in Django that make life easier for one side or the other. For example, template tags allow custom Python code to generate HTML to insert into the page. I use these to generate tab bars and panes, for example.
I really can't help much more without getting a better picture of what your needs are. The question is still vague. You're probably best off taking a day or two going through the tutorial, seeing what the Django perspective is, and then working out how (or if!) it fits into your needs.

Is there any app used for uploading in Pinax?

Is there any app used for uploading in Pinax?
There are different apps included in Pinax that are used for uploading different things: avatars, photos, attachments. The first two are fairly specific but django-attachments is fairly generic if your goal is to attach files to objects.
We also will probably add django-adminfiles in 0.9
You need to write the form and everything yourself (or find a generic django app that does what you want). Pinax is not a CMS where you just attach apps and never actually have to write any code. It gives you an already made social networking site so that you can write the parts that you are interesting in writing (this sounds like something you want to write).