Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
run.py
static/
templates/
tests/
config.py
models.py
I create an instance of my Flask class within run.py, as well as define the views. I am planning on moving the views so they are separate from the run.py file, and just have the run.py file instantiate the Flask class. However, I also have quite a few non-view functions that are called by each of the view functions and implement the application logic.
What is a way to re-organize these view and logic functions to create a good application structure? Should I have a views/ folder with separate view files for each view, including the logic functions that correspond to each of the view functions? Should I group the logic functions together in another, separate folder?
There is some good guidance in the docs. Flask doesn't impose a lot of forced structure but your on the right track for getting in the habit of structuring your projects for your own sanity. This could be entirely left to you how you would like to do this. Probably a lot of opinions out there
What I tend to do is create a structure typical of most python projects. You may want to use an application factory pattern and blueprints.
myapp
/myapp
/home
views.py
/templates
/static
factory.py
core.py
models.py
runserver.py
config.py
Mattupstate has a good blog article on this subject. There is also Fbone. Miguel Grinberg has a chapter in his Flask book dedicated to this too.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have been designing websites with Django for a while and I have also designed various sites. But here is a question that comes to mind at the beginning of any new project:
What is the best a URLs architectural Design that creates both
logical and meaningful URLs path as well as plugable apps?
Parctical Example And Problem
I will explain my question with a practical example. Suppose that we have project with these features:
It has three applications called Shop, Blog and Support.
It has three URL section:
2.1. Public section: that start with /.
2.2. Consumer panel: that start with /panel.
2.3. Administrator panel: that start with /administrator.
Each application has 3 views. For example Shop has: public_shop_view, panel_shop_view and administrator_shop_view.
Now, What is the best URL design for this project? I common solution to this answer:
Solution:
project/urls.py
path('', include('Shop.urls', namespce='Shop')),
path('', include('Blog.urls', namespce='Blog')),
path('', include('Support.urls', namespce='Support')),
Shop/urls.py
path('shop/', views.public_shop_view, name='public_shop_view'),
path('panel/shop/', views.panel_shop_view, name='panel_shop_view'),
path('administrator/shop/', views.administrator_shop_view, name='administrator_shop_view'),
Blog/urls.py
path('blog/', views.public_blog_view, name='public_blog_view'),
path('panel/blog/', views.panel_blog_view, name='panel_blog_view'),
path('administrator/blog/', views.administrator_blog_view, name='administrator_shop_view'),
Support/urls.py
path('support/', views.public_support_view, name='public_support_view'),
path('panel/support/', views.panel_support_view, name='panel_support_view'),
path('administrator/support/', views.administrator_support_view, name='administrator_support_view'),
Now, What is the best practice? Is there any other solution?
Its completely your own choice of selecting the approach... Key point is to keep our code modular enough to be reusable which can be obtained in first approach as every application have its own urls.Py file and can be used in other projects as well by simply importing app or mudules.... So to conclude silution 1 can make your app plugable...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Does django have the same level of code generation like Rails?
In rails you can create models, which then create database migrations.
You can generate controllers, views using the command line.
Django has a very similar way of handling database migrations. They are created by calling python manage.py makemigrations and applied with python manage.py migrate
Controllers (urls.py) must be added manually to each app, but by typing one import statement they are ready to be used.
Views are made automatically when an app is initialized. However, they are empty, so creating a app does not give you the automatic CRUD html that rails does. There is generic class based views in Django which have most of data management done for you, along with automatic form generation, but this requires some actual coding albeit very little.
TLDR: In terms of prefab code generation, Rails wins, but any experienced Django developer can include generic views and forms to get the same functionality in minutes.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm still in the process of learning the basics of Django. I have a lot of questions. But the biggest one at the moment is to understand how I reuse admin models, forms and templates to display it on the frontend instead by going on the backend admin control panel.
What I've done so far is to create a url in urls.py, created a view in views.py and added template path, created the template.
Everything works well with that. But how do I just "copy" the admin model that I want so the user that is logged in can edit on the frontend instead?
What do I need to look more into to understand this so I can implement the admin on the frontend?
Though the admin can be configured through a permission system it is nothing that should be exposed to your front end users - it only should be used by users you can fully trust (administrators).
To implement similar functionality on the frontend look into Django's ModelForms and Generic Views which should help you to implement simple CRUD actions rather quickly. If you would like to implement a Javascript based frontend something like Django-REST-Framework might be a good choice to implement something similar for a REST-API.
Nonetheless you could still add a second AdminSite to your project - but as stated above this is not really recommended for security reasons, if you would expose it to the "normal" user.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm working on a project in which scrapy code just has to crawl data from web and store it into database and django application just need to show it as website, both codes(scrapy and django) have nothing to do with each other. I'm wondering what would be the best practice to setup these two parts of the project? Options are:
Create both of them in same folder
Create scrapy project inside Django project
Guide me. Thanks
As #Pramod has pointed out, there is no necessity that they should be in the same folder. But I'll advise you to take the first option - "Both projects in same folder".
My arguments are:
I'd advice against the second option because, it'd just complicate the django project. As django is a framework, there is a default folder structure and implicit meanings in a django project that is time-tested.
why mess with that?
Its better to have two parts of the project in the same folder as it makes more sense as a project.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am developing a Python App Engine app, where I want to split the content of a source code file Models.py into separate files for each model, but I want to put it all in a folder called Models. The problem is that when I do that, my app can't find the classes anymore. What should I do?
This question is not about MVC but another question with the same title is.
Put an empty __init__.py file in the Models directory.
Then, in your app; presumably one level up, you reference modules in the Models directory like this:
import Models
and do something with it like this:
Models.my_model.MyClassName
You can also use the from keyword like this:
from Models import my_model
and reference like this:
my_model.MyClassName
If you only need one method from a module, you could also do this:
from Models.my_model import my_method_name
my_method_name()
Obligatory link to the documentation.
In response to your comment to Adam's answer, regarding having 10 imports for 10 classes, firstly don't forget that there's no need to have one class per module in Python. Modules should be organised by functionality, so you can group related classes in a single file if that makes sense.
If you still wanted to make all the classes importable in one go, you could import them all in the __init__.py file itself using the from submodule import Class syntax, then just import the containing module - import mainmodule and refer to mainmodule.Class1 etc, or even use from mainmodule import Class1, Class2, Class3 to import the classes directly into your namespace and refer to them directly.
Adam Bernier provides a good technical description of how packages work. A great description of how to arrange and ship a project is described in http://jcalderone.livejournal.com/39794.html