How to create all django apps inside a folder? - python

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

Related

How to run Django FSM first project

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

How to execute external script in views.py in django application?

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.

Django: Startapp Generated files into existing project folder

I want to place django app files into generated Django project folder .
MyProject/
MyProject/
settings.py
urls.py
here i want to insert my Django app files like, views.py, admin.py, models.py etc into same MyProject folder itself. we can do the same by copy paste after app was created. but i want to know is their any magic we can achieve the same while executing this command?
django-admin startapp MyApp <directory>
I dont want to create Myapp folder instead Myapp/* should be move onto Myproject/ itself. without doing copy paste thing.
so finally i want my Project folder should be like this,
MyProject/
MyProject/
admin.py
models.py
settings.py
urls.py
views.py
command to run from same direcory. If you change directory then change in path also:
django-admin startproject myproject
django-admin startapp myapp myproject\myproject\
command sample:
E:\write here>django-admin startproject myproject
E:\write here>dir myproject\myproject
Volume in drive E has no label.
Volume Serial Number is ****-****
Directory of E:\write here\myproject\myproject
03-06-2017 11.53 AM <DIR> .
03-06-2017 11.53 AM <DIR> ..
03-06-2017 11.53 AM 3,103 settings.py
03-06-2017 11.53 AM 766 urls.py
03-06-2017 11.53 AM 396 wsgi.py
03-06-2017 11.53 AM 0 __init__.py
4 File(s) 4,265 bytes
2 Dir(s) 80,954,753,024 bytes free
E:\write here>django-admin startapp myapp myproject\myproject\
CommandError: E:\write here\myproject\myproject\__init__.py already exists, overlaying a project or app into an existing directory won't replace conflicting files
don't worry about above error because it is just notifying you that __init__.py is not to be overwrite.

Adding a second project to a Django project

I'm new to Django. I'm using Django with Eclipse. I've created a Django project using Eclipse (called "Django_Test_Project"). I've also created a PyDev project outside of Eclipse, using the command line (called "polls"). It has models.py, views.py, and tests.py.
I created "polls" using the following command:
manage.py startapp polls
I want use Eclipse to add "polls" as a second project to "Django_Test_Project". How do I do that with a project that was created outside of Eclipse? Eclipse doesn't recognize "polls" as a project, probably because the project files are missing in "polls".
Any help is appreciated. Thanks.
You are working on the Django tutorial, right? First of all, your terminology is not correct. You confuse a project with an application or app for short. It's no surprise that Eclipse doesn't recognize polls as a project, because it's not a project but an app.
In Django 1.4.1, the standard structure for a project called mysite is this:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Your polls app should go in the same directory where the file manage.py is located:
mysite/
manage.py
polls/
__init__.py
models.py
tests.py
views.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
You can just move the polls directory into the mysite directory using the Windows Explorer, Finder, Terminal etc. (depends on which OS you are running on). After refreshing the project view in Eclipse, your polls app should show up. In any case, you should read the Django tutorial more carefully as it basically answers your question already.
Additionally, take a look at this thread that explains the difference between projects and apps in a bit more detail.

Pinax Structure

I have just installed the Pinax but i'm very confused now.
in my structre
i have two directories
mysite and mysite-env
I dont why i have two directories. I just followed the installitation directions
in mysite,
<project-root>
apps/
__init__.py
deploy/
__init__.py
fcgi.py
wsgi.py
fixtures/
initial_data.json
locale/
...
requirements/
base.txt
project.txt
sitemedia/static // I move static file from mysite-env to here
...
templates/
_footer.html
homepage.html
site_base.html
__init__.py
manage.py
settings.py
urls.py
However,
default index page is in mysite-env/lib/python2.6/site-packages/pinax_theme_bootstrap/templates/banner_base.html
Should i manage my site from mysite ?
Pinax uses virtualenv to prevent messing up your local python libs by your project dependency.
So, launch the site with $ source mysite-env/bin/activate it will be fine. If you really hate mysite-env, just skip the virtualenv part.

Categories