How is a django app different from a python package? - python

If I create a normal python package (with __init__.py), instead of manage.py startapp won't I still be able to use it like a django app.?

Django app is actually a python package that follows the Django convention. Django-admin startapp is just a helper command to create the files in that convention. If you want to create an app without using startapp, then can create a folder and create __init__.py file and create the necessary files(for views and models). And you should include it in the INSTALLED_APPS. That's all.

Yes, you will be able to use it as a django app. Django is a web framework, hence its main aim is to allow their users to focus on their applications rather than to make them hard-code every single bit of information.

Related

Django and pure Python

I want to create a web app with Django and integration of Selenium and Python-Docker API.
Where inside the Django project is the correct place to put the Selenium scripts so users will be able the run it by a puse of a button?
I've seen some thred saying inside a 'view', but is is a lot of logic that I've sprated into multipale .py files.
You must import Selenium package inside another file such as sel-functions.py inside your app.
Then you define your class or functions inside this file. Finally you can import all those functionality to your views.py file.
This is best and cleanest way to work with another package or functionality in Django project.
Also if multiple apps of your project need to sel-functions.py you can put that in your project directory beside settings.py.
Also, if your code has grown so much that it has multiple files, you
need to define a package for your intended work and define it as an
application in your project.

Reusable Django Application Uninstallation

I am creating a Django reusable app, which creates some models and performs certain views on those models.
So I make it as a python package and use it in an independent project. At some point, if I want to uninstall this app, it should remove the tables that it had created in the project's database.
Is it possible to make my python package in such a way to remove all of my app's footprint?

What is the purpose of adding to INSTALLED_APPS in Django?

Most documentation simply tells you to add the name of each of your apps to the INSTALLED_APPS array in your Django project's settings. What is the benefit/purpose of this? What different functionality will I get if I create 2 apps, but only include the name of one in my INSTALLED_APPS array?
Django uses INSTALLED_APPS as a list of all of the places to look for models, management commands, tests, and other utilities.
If you made two apps (say myapp and myuninstalledapp), but only one was listed in INSTALLED_APPS, you'd notice the following behavior:
The models contained in myuninstalledapp/models.py would never trigger migration changes (or generate initial migrations). You wouldn't be able to interact with them on the database level either because their tables will have never been created.
Static files listed within myapp/static/ would be discovered as part of collectstatic or the test server's staticfiles serving, but myuninstalledapp/static files wouldn't be.
Tests within myapp/tests.py would run but myuninstalledapp/tests.py wouldn't.
Management commands listed in myuninstalledapp/management/commands/ wouldn't be discovered.
So really, you're welcome to have folders within your Django project that aren't installed apps (you can even create them with python manage.py startapp) but just know that certain auto-discovery Django utilities won't work for that application.

Can I put my views.py file in the project folder of django?

I am developing a project using django python server. I have created my project on django and put all my files including views.py in the project folder and I am using it without creating any app and its working fine.
Is this the right way of doing it (or) I need to create an app instead and put all my files in the project ?
This will work fine. Views can be wherever you want.
You can add the package that is your site (the one that has settings.py in it) to INSTALLED_APPS, and then a models.py in it, management commands, et cetera will also work fine.
Apps are handy when things become big and you want to split them into smaller parts.

how to perform migration for django table created without any app

I have a django project in which I have not created any app, I directly wrote models for the project in models.py file inside project folder.
But whenever I perform
python manage makemigrations
it says : No changes detected
so when I perform
python manage migrate
it says : No migrations to apply.
So is there any different way to perform migrations if you are directly writing your models without creating any App for django project
You should add your app to django INSTALLED_APPS settings, if not, then, you dont have an django app.
migration on django is an app basis.
migrations are created and run on a per-app basis, https://docs.djangoproject.com/en/1.8/topics/migrations/
so you should create an app and add it to INSTALLED_APPS as levi suggest. However, it is possible if you're kind a want to only use one app to manage all the models for your db under models.py, BUT it's messy.
i recommend to use the migration on app basis.

Categories

Resources