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
Related
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
I got an error which is a template that does not exist while I was created a template folder that contains a file. I had done necessary changes in setting.py file.
This should be the hierarchy of your project. Django detects templates like this:
manage.py
project_name/
wsgi.py
urls.py
settings.py
web/
urls.py
models.py
views.py
templates/
web/
home.html
newhome.html
For further info, checkout documentation : https://docs.djangoproject.com/en/2.2/intro/tutorial03/
You should move your templates directory to the root of the project, besides manage.py
Basically, thee BASE_DIR is where your manage.py file lives; you add os.path.join(BASE_DIR, 'templates') into your templates directories, meaning one level up.
on your Templates setting,remove the catalog and edit to this
os.path.join(BASE_DIR,'templates')
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 have a Django project on a Centos VPS.
I created some models and debugged them so they validate and give no errors.
I have them in a "models" folder in my myapp and have added each model to the init file in this directory, for example:
from category import Category
I added the app to settings.py INSTALLED_APPS and ran:
Python manage.py syncdb
It appeared to work fine and added all tables apart from the ones for my app.
I then installed South and added that to INSTALLED_APPS and, tried syncdb again and ran:
Python manage.py schemamigration myapp --initial
It generated the file correctly but nothing was in it (none of the tables my models).
An example file in "models" folder (usertype.py)
from django.db import models
class UserType(models.Model):
usertype_id = models.PositiveIntegerField(primary_key=True)
description = models.CharField(max_length=100)
is_admin = models.BooleanField()
is_moderator = models.BooleanField()
class Meta:
app_label = 'myapp'
Any ideas what could be going wrong here and why I can't get anything to detect my models?
Run the following commands
python manage.py makemigrations yourappname
python manage.py migrate
Note it works on django 1.7 version for me.
you're misunderstanding the process of working with south. South isn't just another application, it's a managing tool. Your app needs to be a South application from the begining or converted to one. That being said, the process is like so:
Add South to INSTALLED_APPS
run syncdb for the first time
Add your app to INSTALLED_APPS*
run the south initialization command:
python manage.py schemamigration myapp --initial
migrate:
python manage.py migrate
If you want to convert a project:
Run syncdb after adding south
run:
manage.py convert_to_south myapp
And use south from now on to manage your migrations.
*p.s. - you can add both south and your own app at the same time, if you keep in mind to put south before your own apps. That's because django reads INSTALLED_APPS in order - it runs syncdb on all apps, but after installing south it won't install the rest and instead tell you to use the south commands to handle those
edit
I misled you. Since you put so much emphasis on the south thing I didn't realize the problem was you were trying to use models as a directory module instead of a normal file. This is a recognized problem in django, and the workaround is actually exactly as you though in the first place:
say this is your structure:
project/
myapp/
models/
__init__.py
bar.py
you need bar.py to look like this:
from django.db import models
class Foo(models.Model):
# fields...
class Meta:
app_label = 'myapp' #you need this!
and __init__.py needs to look like this:
from bar import Foo
Make sure it looks like this and it will work.
UPDATE 18/08/2014
The ticket has changed to wontfix, because apparently the bigger issue with the app_label has been fixed. Huzza!