I have below django-program--- walk.py
from django.db import models
from django_fsm import transition, FSMIntegerField
from django_fsm import FSMField, transition
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
import django
django.setup()
from django.core.management import call_command
class Order(models.Model):
STATUS_GO = 0
STATUS_COME =1
STATUS_CHOICES = (
(STATUS_GO, 'GO'),
(STATUS_COME,'come')
)
product = models.CharField(max_length=200)
status = FSMIntegerField(choices=STATUS_CHOICES, default=STATUS_GO, protected=True)
#transition(field=status, source=[STATUS_GO], target=STATUS_COME)
def walk(self):
print("Target moved")
Above code is available in c:\Hello folder.
I have referred few blogs & link for creating new django project.
so in cmd window, dragged to above folder by "cd c:\Hello" & executed:
django-admin startproject mysite
And moved walk.py into mysite folder
And the directory as :
Hello/
mysite/
manage.py
walk.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
And Later executed:
python manage.py migrate
python manage.py runserver
Post which i have been stuck now & confused above two steps is required for my project.
Do
python manage.py startapp polls
is mandate to run now ? If so, what to edit in polls/models.py file ??
Later what do i need to mention in INSTALLED_APPS = [] ???
And keep moving further, where do i place my project walk.py in above directory ?
Now when i run the walk.py , i could see below issue now:
RuntimeError: Model class main.Order doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
Pls any help
In INSTALLED_APPS you will have to add the new app like this:
INSTALLED_APPS = [
// ...
'django.contrib.staticfiles',
'django.contrib.sites',
'polls'
]
Now Django will be aware of your app.
Actual error :
RuntimeError: Model class main.Tag doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
my Solution:
run
django-admin startproject mysite
in the project folder
Hello/
mysite/
manage.py
walk.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Later added ' main' in INSTALLED_APPS & works fine now
Related
I started a project and created an app using
django-admin startproject tutorial
django-admin startapp snippets
Now, I am adding the "snippets" app to the installed_apps in settings.py.
but I m not able to understand the difference between:
"snippets" and "snippets.apps.SnippetsConfig"
Which line to add in the installed_apps in settings.py file.
I have created a Django project using this directory structure :
bi3online
__init__.py
migrations
__init__.py
static
templates
media
manage.py
models.py
urls.py
views.py
wsgi.py
...
When I run python manage.py makemigrations and python manage.py migrate it only creates migrations for auth app and then when I try again it says no changes detected. It seems that migrations work only with apps but I'm not sure.
models.py
from django.db import models
class Book(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(null=False, blank=False, max_length=64)
Just to sum up the comments and lead you to the solution:
Django works with apps. The app is a folder with models.py, apps.py and views.py modules (optionally other).
Say your app is called books. For Django to recognize the folder as an app you need to add your app to settings.py:
INSTALLED_APPS = [
... ,
'books'
]
And you need your apps.py module in books folder to have this:
from django.apps import AppConfig
class BooksConfig(AppConfig):
name = 'books'
And you should have the following project structure:
bi3online
books
migrations
__init__.py
__init__.py
models.py
views.py
bi3online
__init__.py
manage.py
urls.py
wsgi.py
static
templates
media
Here is my project folder structure:
.venv [virtual environment]
apps
budgetApp
__init__.py
settings.py
urls.py
wsgi.py
manage.py
When I run the following command python manage.py startapp budget it creates a new folder named budget beside the folder budgetApp.
But I want to create all my apps folder inside the apps folder.
At first, you need to create a directory Your_App_Name inside the /apps folder.
After that, run the following command to create the new app
python manage.py startapp Your_App_Name ./apps/Your_Apps_Folder_Name/
Then don't forget to add just created app name in the settings.py like below:
INSTALLED_APPS = [
...,
'apps.Your_App_Name',
]
You can specify the path to destination directory after app_label in the startapp command.
python manage.py startapp <app_label> [destination]
In your case you the command is like this:
python manage.py startapp budget ./apps
Then you should add just created app name in settings.py like below:
INSTALLED_APPS = [
...,
'apps.budget',
]
You can also do it like this
cd apps && django-admin startapp app_name
First run python manage.py startapp budget app/budget
Then, on your settings.py, change the INSTALLED_APPS to the full path name:
INSTALLED_APPS = [
'app.budget.apps.BudgetConfig',
]
When you use a nested structure for apps, you also need to change the name of the app in apps.py as follows:
class BudgetConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.budget'
Check https://code.djangoproject.com/ticket/24801 for more information.
If you're on Windows, you'll first need to create a folder app/budget. Then add the file __init__.py to the folder add. Then run the command py manage.py startapp budget app/budget.
Then, on your settings.py, change the INSTALLED_APPS to the full path
name:`INSTALLED_APPS = [
...,
'app.budget',
]`
also need to change the name of the app in apps.py as follows
class BudgetConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app.budget'
I am making the following assumptions:
You are on a Unix/Linux PC
Your app is named: perstep
1. mkdir -p apps/perstep && touch apps/__init__.py
2. python manage.py startapp perstep ./apps/perstep
OR
1. mkdir -p apps && touch apps/__init__.py
2. cd apps && django-admin startapp perstep
configurations
reference the added diagram when in doubt.
1. In settings.py "INSTALLED_APPS" add "apps.perstep.apps.PerstepConfig" to it.
├── apps
│ ├── __init__.py
│ └── perstep
│ ├── apps.py
2. Change name = 'apps.perstep' within 'apps > perstep > apps.py' PerstepConfig
This works as at Django==4.0.5
Following is my django file structure
myproject
myapp
migrations
static
templates
myapp
demo.html
urls.py
views.py
models.py
admin.py
apps.py
tests.py
Scripts
demo_script.py
manage.py
What I want to know is how to execute the script Scripts/demo_script.py (which is outside the application "myapp") from views.py and if it is executed successfully, show "Success" on myapp/templates/myapp/demo.html or else show "Failed to execute" on the same html page.
Thanks in advance!
simply use:
relative_path = "scripts/demo_script.py"
# or
absolute_path = os.path.join(settings.BASE_DIR, "scripts", "demo_script.py")
exec(open(relative_path or absolute_path).read()) # execute the py file
All the paths referenced in Django are relative to the BASE_DIR. BASE_DIR is where your manage.py lies.
I am making a reusable Django app without a project. This is the directory structure:
/
/myapp/
/myapp/models.py
/myapp/migrations/
/myapp/migrations/__init__.py
When I run django-admin makemigrations I get the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Obviously, this is because I don't have a settings module configured, because this is a reusable app. However, I would still like to ship migrations with my app. How can I make them?
Actually, you don't need to have project, all you need is settings file and script, that runs migrations creation.
Settings must contain folowing (minimum):
# test_settings.py
DEBUG = True
SECRET_KEY = 'fake-key'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'your_app'
]
And the script, that makes migrations should look like this:
# make_migrations.py
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_settings")
from django.core.management import execute_from_command_line
args = sys.argv + ["makemigrations", "your_app"]
execute_from_command_line(args)
and you should run it by python make_migrations.py. Hope it helps someone!
You need a functional Django project (with your app installed in it) to make migrations.
A common way to do this is to have a "test" project which contains the bare necessities of a Django project, that you can run to make migrations etc. The migrations will be created in the right place inside your app directory so you can still have proper version control etc within your own reusable app.
The migrations created in this way will be self-contained (assuming your models don't depend on models from other apps) and can be shipped as part of your packaged, reusable app.
Many of the larger Django-based projects actually ship a test project as part of their code, so that developers can quickly get it running in order to test apps and make migrations etc.
Create your_app/migrations_settings.py file:
SECRET_KEY = 'fake-key'
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'your_app'
]
then
export DJANGO_SETTINGS_MODULE=yourapp.migrations_settings
django-admin makemigrations yourapp
Real Python has a tutorial on writting a reusable Django App.
The chapter Bootstrapping Django Outside of a Project has a very interesting example:
#!/usr/bin/env python
# boot_django.py
"""
This file sets up and configures Django. It's used by scripts that need to
execute as if running in a Django server.
"""
import os
import django
from django.conf import settings
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "your_app"))
def boot_django():
settings.configure(
BASE_DIR=BASE_DIR,
DEBUG=True,
DATABASES={
"default":{
"ENGINE":"django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
},
INSTALLED_APPS=(
"django.contrib.auth",
"django.contrib.contenttypes",
"your_app",
),
TIME_ZONE="UTC",
USE_TZ=True,
)
django.setup()
And then:
#!/usr/bin/env python
# makemigrations.py
from django.core.management import call_command
from boot_django import boot_django
boot_django()
call_command("makemigrations", "your_app")