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.
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')
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
having done python manage.py collectstatic for my Django application I copied the admin assets into the static folder. However when it comes time to view the admin module in the Django application the assets aren't viewable and instead I see a 404 GET trace back to the admin assts. These are the current Handlers that I have registered with the Tornado application that I am deploying:
('/hello-tornado', HelloHandler),
(r"/static/(.*)", tornado.web.StaticFileHandler, {"path": "/home/django-tornado-demo-master/testsite/static"}),
('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
These is the file tree of my statics folder:
static
└── admin
├── css
├── img
│ └── gis
└── js
└── admin
How can I modify the Static File handler to load the assets that I have in this tree structure? Thanks for solutions.
In the end I found out that my problem was that the static directory referenced the wrong 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.