How to run a django project without manage.py - python

Basically I downloaded django project from SCM, Usually I run the project with with these steps
git clone repository
extract
change directory to project folder
python manage.py runserver
But this project does not contains manage.py , how to run this project in my local machine???
br

You'll have to create a manage.py file manually if you wanted to use its commands in the same way you're accustomed to. You can modify django's manage.py template to include your project's settings.
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)

Most likely, this is not supposed to be a complete project, but a plugin application. You should create your own project in the normal way with django-admin.py startproject and add the downloaded app to INSTALLED_APPS.

You can use uwsgi to run a django project.
First install uwsgi using:
pip install uWSGI
Go to project folder and enter this in terminal:
# Replace server with whatever is your project name
uwsgi --http :8000 --module Server.wsgi

First create a virtual environment and install Django. Now you have django-admin.py available in your system.
django-admin is Django’s command-line utility for administrative tasks.
$ django-admin startproject name [directory] create a Django app directory structure for the given app name in the current directory or the given destination.
You can provide the path to a directory with a custom app template file or a path to a compressed file (.tar.gz, .tar.bz2, .tgz, .tbz, .zip) containing the app template files.
$ django-admin startproject --template=/Users/jezdez/Code/my_app_template myapp
Django will also accept URLs (http, https, ftp) to compressed archives with the app template files, downloading and extracting them on the fly.
For example, taking advantage of GitHub’s feature (or other SCM) to expose repositories as zip files, you can use a URL like:
$ django-admin startproject --template=https://github.com/githubuser/django-app-template/archive/master.zip myapp
$ cd my_proj
$ touch manage.py
Put this content into manage.py like #Sayse said:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
# CHANGED manage.py will use development settings by
# default. Change the DJANGO_SETTINGS_MODULE environment variable
# for using the environment specific settings file.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings.development")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Install dependencies and run migrations.
$ pip install -r requirements.txt
$ python manage.py migrate
$ python manage.py runserver
Hope I help!

Related

How to run Flask app as a package in PyCharm

I was following this documentation on directory management for Flask projects. Now, I'm trying to run my flask application from PyCharm. I have added the below mentioned Environment Variables in Edit Configurations...:
FLASK_DEBUG=true
FLASK_APP=<absolute-path-to-root-directory-of-application>
I add the Script as flask run
The output running this configuration is this:
../red-flask/venv/bin/python "flask run"
../red-flask/venv/bin/python: can't open file 'flask run': [Errno 2] No such file or directory
Process finished with exit code 2
My project directory looks like:
/flask_app
setup.py
/flask_app
__init__.py
views.py
/static
style.css
/templates
layout.html
index.html
login.html
...
I am unable to figure out how to make this work, any help is appreciated.
This is documented in the development build of the docs.
You need to point to the location of the flask command.
Script: /path/to/env/bin/flask
Script parameters: run
Until 1.0 comes out, you need to point FLASK_APP at __init__.py if you don't install your package in your env.
Environment variables: FLASK_APP /path/to/flask_app/__init__.py
Preferably, install the package in the env and point to it using the import name.
From the terminal, in the virtualenv: pip install -e .
Environment variables: FLASK_APP flask_app

Can't seem to setup my Django project properly

I was trying the official tutorial of Django because I want to learn it. I run Archlinux 4.10.11-1 64 bits.
First, I created a folder named djangoapp where I set my virtual environment:
$ virtualenv djangoapp
I set my current directory to this folder, and then I activated it:
$ source bin/activate
And I installed Django after:
$ pip install django
Following the tutorial, I ran:
$ django-admin startproject djangoapp
And set my current directory to djangoapp, and ran:
$ python manage.py runserver
But I'm getting the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Indeed, running env | grep DJANGO_SETTINGS_MODULE gets me the following result:
DJANGO_SETTINGS_MODULE=
So I tried to set it manually:
$ export DJANGO_SETTINGS_MODULE='djangoapp.settings'
Running $ python manage.py runserver now works, but $ django-admin help now gets me the following error:
ModuleNotFoundError: No module named 'djangoapp'
What did I do wrong? Thanks for your help!
PS: $ python --version gets me Python 3.6.1.
I think the docs could be a little clearer about this, but django-admin is typically only used for running django-admin startproject and manage.py is used for the rest e.g. ./manage.py runserver, ./manage.py migrate etc.
Deleting the DJANGO_SETTINGS_MODULE environment variable should allow you to run both ./manage.py * and django-admin startproject commands when inside the djangoapp folder.
I'm not sure how to do this on Archlinux, but something like unset DJANGO_SETTINGS_MODULE or set -e DJANGO_SETTINGS_MODULE should work.
Further info: you can think of manage.py as a wrapper around django-admin that automatically sets the PYTHONPATH and DJANGO_SETTINGS_MODULE environment variables. Therefore, once inside the project, you can just use ./manage.py rather than django-admin to run the management commands.

Git command when translating files in Django

I have an existing application in Django.
I want to add a translation on the page.
On page I have:
{% trans 'Projects'%}
In .po file I added:
#: templates/staff/site.html: 200
msgid "Projects"
msgid "Projekty"
Then executes the command:
django-admin.py compilemessages -l pl
After this command, I get an error:
CommandError: This Should Be Run script from the Django Git checkout or your project or app tree, or with the settings Specified module.
$ python manage.py compilemessages --settings nsp.settings
CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.
I have got this error while I truly was inside project root folder.
The problem was, that I was running this command without python manage.py makemessages first.
The error message is misleading.
The error holds the answer, you could be running the script from anywhere so it cannot know which files to compile. Run the command from the project directory or specify the settings and you should be fine.
If you are using docker containers to build and deploy your application you should copy folder:
conf/
from root folder of your django project.
with the conf folder you should see i.e:
processing file django.po in /gamma/conf/locale/en/LC_MESSAGES
processing file django.po in /gamma/conf/locale/es/LC_MESSAGES
processing file django.po in /gamma/conf/locale/pt_BR/
without the conf folder you should see a clueless message like that:
CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.
The error message is saying that it could not find the translations files where it expected them to be. Check that everything is correctly setup:
LOCALE_PATHS is defined in your settings.py
the files exist in the folder defined above (created by running python manage.py makemessages)
actually the error goes away even with just an empty locales folder
the compilemessages command is run from the project root folder
If you haven't set LOCALE_PATHS in your settings file, you need to do so:
import os
LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]

Issues in initial setup for django project

I am learning Django from the official documentation and while going through the tutorial at https://docs.djangoproject.com/en/1.7/intro/tutorial01/, I am stuck at creating a project part.
When I run django-admin.py startproject mysite I am getting following error
C:\Python34>django-admin.py startproject mysite
Usage: django-admin.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Raise on exception
--no-color Don't colorize the command output.
--version show program's version number and exit
-h, --help show this help message and exit
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runfcgi
runserver
shell
sql
sqlall
sqlclear
sqlcustom
sqldropindexes
sqlflush
sqlindexes
sqlinitialdata
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
syncdb
test
testserver
validate
Note that only Django core commands are listed as settings are not properly configured
error: Requested setting INSTALLED_APPS, but settings are not configured
. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
I am using Python 3.4.1 and django 1.7. I don't have any other Django version installed and this is the first project I am creating.
You can just run django-admin startproject mysite(Note: not django-admin.py), because if you install django by pip, a executable program named 'django-admin.exe' will be added to 'C:\Python34\Scripts', which is already in your environment variable 'PATH' normally(if not, add it to PATH).
I was facing the same issue while installing Django 2.0.5. This can be resolved using Virtual Environments.
Environment details:
Python version: 3.6
OS: Ubuntu 18.xx.x
Steps:
Install Virtual Environment
pip install virtualenv
Create a Virtual work-space('VEnv')
virtualenv --python=python3 VEnv
Activate the Virtual Environment:
cd VEnv/
source bin/activate
Install Django(Version - 2.0.5)
pip install django==2.0.5
Create Project (Project Name: HelloDot)
django-admin startproject HelloDot
Run the server as below and then access it from "http://127.0.0.1:8000/"
cd HelloDot/
python manage.py runserver 8000
For more details, take a look at this: https://www.howtoforge.com/tutorial/how-to-install-django-on-ubuntu/
Make sure that you follow the troubleshooting guide because it looks like you don't have django-admin.py on your system path correctly. From the docs:
django-admin.py should be on your system path if you installed Django
via python setup.py. If it’s not on your path, you can find it in
site-packages/django/bin, where site-packages is a directory within
your Python installation. Consider symlinking to django-admin.py from
some place on your path, such as /usr/local/bin.
You should also use a virtualenv for each of your projects to allow isolation of dependencies per project and easier management of them. virtualenvwrapper is a useful tool for creating and managing your virtualenvs.

Exist a diference in use django-admin.py or manage.py

I'm learning something about Django.In some tutorials that i've read is used django-admin.py for execute commands like " syncdb, startapp and startproject", in the others tutorials is used manage.py, So exist any important diference between use django-admin.py or manage.py ?
See: django-admin.py and manage.py in Django documentation
manage.py is a wrapper around django-admin.py automatically created in each Django project which puts your project's package on sys.path and sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project's settings.py file.
So, I think is more comfortable to use manage.py when you're working on a Django project.
manage.py is a file that excists in a project environment and is created after starting a project. It's purpose is to control project commands. Django-admin.py is a script to start a project ( you can put it anyhwhere, or shortcut it via .bash or .profile )
manage.py is django-admin fitted in your project: it uses settings.py in your project dir

Categories