I am new to Django. I want to include rest api to my project using tastypie framework. I want to know what are best practices of including tastypie in project. I see two options:
Separate app where all api related modules are stored (this app will end up rather fat)
api folder in every app with api resources for this particular app (not sure that understand how to organize urls in this case)
So what is the best practice for this?
Thanks
Actually I prefer the first approach. I create an api folder and then i create subfolders for each separate application. Each folder contains an api.py file for the specific need.
This way it is really simple and clear to support versions in the api and make it more clear. Also in the api folder i keep a urls.py which contains all the apis from the subfolders and it is easier to maintain.
EXAMPLE
api/
app1/ api.py
app2/ api.py
urls.py
and the urls.py should look a lot like,
from tastypie.api import Api
from .app1.api import App1Resource
from .app2.api import App2Resource
api = Api(api_name='myPath')
api.register(App1Resource())
api.register(App2Resource())
urlpatterns = api.urls
This is of-course just my personal preference which have helped me over the years to maintain a variety of different apis.
I hope i helped a bit.
Regards,
Michael.
Related
After some years developing web apps using ruby on rails, I decided to give Django a try, however it seems that I'm missing something, which is how to structure large project, or any project in general.
For example, in rails we have a models folder which contains model classes, each in a separate ruby file, a controllers folder which contains controller classes, again each in a separate ruby file.
However, in Django it split the project into independent apps, which can be installed independently in other Django project, each app has a models.py file which contains all the models classes, a views.py file which contain all the views functions.
But then how to group functions in views like rails? That is one controller per each model.
In general how to structure my project when it contains one large app that can't be separated into multiple independent apps? I want for example to have a view index function for each model, but how to do this if all functions are in one file?
If my project is about selling cars for example. I should have index function that maps to /cars, another index function to map to /users, etc...
I searched the web but couldn't find a suitable answer.
It is unclear to me how to structure Django app, so any help will be appreciated.
As mentioned in #shanksfk's answer, Django is very flexible in folder arrangements. You don't have to follow the default app structure. When I create a purely backend Django project (with DRF), I usually have 3 base apps:
api - where modules, serializers, and urls are stored
core - the default app (the one that has the name of your Django project)
db - where models are stored
Then as I expand, I can add a folder dedicated for the helpers, utils, and possibly abstraction layers for external services. I recommend reading more about Domain-driven Design to get an idea on how to structure your project. You can also check other Django projects for inspiration:
django CMS
Baserow
Django API Domains
In short, Django is a Model-View-Template framework and Rails is a Model-View-Controller framework.
In Django we store controllers(sort of) in views.py for each specified app, while in MVC framework such as Rails store it in controllers. In Django, you also have to create your own HTML template separately which some people may find it tedious but its easier to work with other frameworks such as Vue or React due to that separation.
This is general comparison I found on the net.
However, to answer your questions on folder structure. Basically Django is very flexible on folder arrangements, it really depends how you want to design the project structure. Normally what I'd do is keep every app in the main folder (project folder). This way you wont mess with the venv setup
I want to start a new django project. This will just be a relatively simple website to test django itself. So the way all tutorials structure their project is this:
mysite
mysite
some_app
...
For me it's not very clear what does what. I don't really need any app right now. Could I implement my website without using any app? Should I use an app? What would it be called for a simple website?
Have you read Django doc already?
They have a nice tutorial on how to get started.
As for apps, in Django, you have a project containing one or more app. If you just want to try to build a simple website, your project called mysite will contain only one app.
I think you don't need more than the main mysite, BUT, at the django documents, have a session that says:
If your background is in plain old PHP (with no use of modern
frameworks), you’re probably used to putting code under the Web
server’s document root (in a place such as /var/www). With Django, you
don’t do that. It’s not a good idea to put any of this Python code
within your Web server’s document root, because it risks the
possibility that people may be able to view your code over the Web.
That’s not good for security.
Put your code in some directory outside of the document root, such as
/home/mycode.
This model may have been designed with focus on safety.
Some files in mysite could be used to control things in another apps, so i think that is a 'best pratice' to follow this model.
Font
if u aren't in need of an app you can avoid creating one. just give your view in views.py of your project set the url and u r all set to go.
here the first mysite folder is just an usual folder u can change the name in ur convenience next mysite is your project folder and recommended not to change it and next some_app is the application name(you can name your app anything relevant but in proper convention.)
and follow django documentation and djangogirls tutorial for better understanding.
https://docs.djangoproject.com/en/2.0/
https://tutorial.djangogirls.org/en/
i'm a bit of a newcomer to Django/Python but trying to figure something out--hope you can help.
I'm trying to create a personal website with a page dedicated to some of the Python and Django projects that I've completed through several different online courses.
I'm just having a tough time figuring out the best way to link through and assemble the project directories on my server.
My first project is just the files for my blog itself. I've created a new directory in the same home root as the blog project housing another of my django projects. Just looking for a bit of assistance on how i link out to the second project from my blog.
I thought i could use the urls.py file for my blog to redirect link to the second project (ie projects/project2) utilizing a views definition from the views.py file for one of the apps in the blog. But then--i'm getting tripped up on how to render that link to the second project.
Any forward guidance is greatly appreciated.
In general, anything that makes sense to be served under different domain (not necessarily subdomain) would better be a separate project for sure. Exceptional case would be a project with almost same functionality but different branding within same organization. In such case, Django's built-in sites framework can be considered.
For different projects, you need different project roots along with different wsgi/port bindings and processes. They can still be listed within a single nginx configuration as a deployment example. Another popular way is using Docker but deployment methods vary.
For different apps in a single project, there is only one binding and one root already. Create apps and list them in your settings.py and urls.py. If you need subdomains with single deployment, tkaemming/django-subdomains might be helpful.
For different policies (rarely) in a single app under different domains, learn about sites framework. You need to hand code the difference in views.
and so on...
I'm new to CherryPy, coming from Django. I liked the way Django split the various parts of the project into many files, and I'd like to do the same in CherryPy, instead of having one big file.
I think it would be great if I could split the project in these parts:
application.py : The core of CherryPy, where the server starts
urls.py : Containing all the urls, probably using RoutesDispatcher
models.py : Via SQLAlchemy, containing all the models
controllers.py : Pretty obvious ;)
I'm not asking for a complete code paste here, a link to an example/pastebin/gist is perfect too :)
Thanks for your help.
Well, I'll answer myself on that one :p
I found a Google Groups post that poses the basics on how to structure your CherryPy application.
In that post, there is a link to a Bitbucket CherryPy application example that is very well structured and could be used as a base example for any CherryPy application.
I have been reading some django tutorial and it seems like all the view functions have to go in a file called "views.py" and all the models go in "models.py". I fear that I might end up with a lot of view functions in my view.py file and the same is the case with models.py.
Is my understanding of django apps correct?
Django apps lets us separate common functionality into different apps and keep the file size of views and models to a minimum? For example: My project can contain an app for recipes (create, update, view, and search) and a friend app, the comments app, and so on.
Can I still move some of my view functions to a different file? So I only have the CRUD in one single file?
First, large files are pretty common in python. Python is not java, which has one class per file, rather one module per file.
Next, views, even as the standard used, is a python module. A module need not be a single file. It can be a directory containing many files, and __init__.py
And then, views.py is only a convention. You, the application programmer are referring to it, and django itself doesn't refer anywhere. So, you are free to put it in as many files and refer appropriate functions to be handed over, the request to, in the urls.py
They don't have to go in views.py. They have to be referenced there.
views.py can include other files. So, if you feel the need, you can create other files in one app that contain your view functions and just include them in views.py.
The same applies to models.py.
Django apps lets us separate common
functionality into different apps and
keep the file size of views and models
to a minimum? For example: My project
can contain an app for recipes
(create, update, view, and search) and
a friend app, the comments app, and so
on.
I don't know about the "to a minimum" part - some apps are just big in views, others big in models. You should strive to partition things well, but sometimes there is just a lot of code. But other than that, this is a fair summary of Django apps, yes.
I also very much dislike long files.
Of course what you read in the other answers is true, but I exploit some very nifty python equivalence:
views.py
and
views/__init__.py
are pretty much functionally equal - by that I mean that if the both contain def my_view() then
from views import my_view
will work the same in both cases!
From there it's easy to structure your long files into smaller ones, yet keeping the naming convention that every django developer is used to:
views/__init__.py
views/largemodel_view.py
then in __init__.py don't forget to import the views from largemodel_view.py.
With large applications I do the same with models though you must remember to set the Meta.app_name:
class MyModel(models.Model):
...
class Meta:
app_name = 'yourappname'
because django will not pick it up magically otherwise for the Admin (but it will still load it, thanks to Python!)
so my apps usually end up looking something like:
project/settings/__init__.py
/..othersettings..
/app_1/models/__init__.py
/...
/views/__init__.py
/...
/templates/
/static/
urls.py
/urls.py
etc.
though of course there's no limit (urls could be split too, etc.etc.)