gunicorn and django project (no app) - django

I have a django project, and I want to deploy it with gunicorn/nginx.
But I use settings.py urls.py views.py directly without start a django app, and gunicorn needs application name.
How can I deploy a django project which has no app.

You don't need an application name with gunicorn_django command. just run gunicorn_django command in your projetc folder or tel him the path of settings
http://gunicorn.org/run.html#gunicorn-django

Related

Traefik and Django on a Subpath

Following setup I want to achieve inside Docker with Traefik and Django:
http://domain/app1
http://domain/app2
My docker-compose.yml contains the following labels for the containers:
traefik.http.routers.app1.rule=Host(`my.host.de`) && PathPrefix(`/app1`)
traefik.http.routers.app1.middlewares=app1
traefik.http.middlewares.app1.headers.customresponseheaders.SCRIPT_NAME=/app1
I did the same for app2.
In the settings.py of both apps I set: FORCE_SCRIPT_NAME = env('FORCE_SCRIPT_NAME', default=None) which then should get resolved via the ENV File where I have FORCE_SCRIPT_NAME=/app1.
On Django side I always get a 404 with the message that this path does not exist and I should choose from an existing one.
Django recognizes the URL as http://my.host.de/app1 and tells me The current path, app1, didn't match any of these.
EDIT: Since my setup is thought to be both for dev and prod envs, I am using the Django built in server as well as Gunicorn for Running the Django apps.
If you want to pass a SCRIPT_NAME header to django, you have to use customrequestheaders instead of customresponseheaders when creating the middleware

Active Django app do not recognize global $PATH

I have deployed Django project (coded in Windows) on Ubuntu 18.04 server with gunicorn and nginx. There is a view which call in command line external package executable, located in /home/user/blast/bin/. PATH to package is defined in .profile
export PATH="$PATH:/home/user/ncbi-blast-2.8.1+/bin"
Package PATH is seen in echo $PATH also in my django project virtualenv.
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ka4an/ncbi-blast-2.8.1+/bin
I am able to run package executable in some_script.py in main shell and in django virtualenv like this:
subprocess.run("package_executable", shell=True, stdout=log_file, stderr=log_file)
and it returns expected output. However, when It is run in active Django app through views.py it returns that package_executable is not found. I found that inside active Django app i have different echo $PATH output, without PATH to my package:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Active Django app is run by the same root user (who - returns same user). I can run my package inside active Django app by providing full path to package executable, but i`m wondering in why Django do not recognize $PATH defined in .profile.

Why won't newly installed Django app with NGINX serve static assets properly?

I have a Mac running OS X 10.9.3. I am trying to setup a Django application backed by a PostgreSQL database served by gunicorn, with static assets served by NGINX. I'm an old hand at Django with MySQL running with the developement server (manage.py runserver). But I'm new to setting it up with virtualenv, gunicorn and NGINX. So I'm following the instructions here.
My Django Project is being served successfully at localhost:3026. As a test of the database connectivity, I wanted to take a look at the Django Admin interface. I visited localhost:3026/admin/
I have included a screenshot below.
Why does this admin page look so ugly? It lacks the neccessary graphical interface and css that it is supposed to have? It looks like NGINX is not properly serving up those static assets. How can I troubleshoot and fix this issue?
EDIT:
After I posted this question, I did python manage.py collectstatic. That went and successfully copied all the static files to where they were supposed to (I think?) live in /opt/myenv/static. You can see the output of that command here. I then re-started gunicorn and nginx. I thought that would fix it. But unfortunately it didn't. The issue remains. In my Django settings.py file, I have configured the STATIC variables as follows:
STATIC_ROOT = "/opt/myenv/static/"
STATIC_URL = '/static/'
Try run command,
python manage.py collectstatic
If the commands executes successfuly, the static file would be generated in your project path, and then if you config the right static path, the web page will be correct.

Django can't load app

I'm using django 1.4, after i created the project using django-admin, I create an app, add it to settings.py in INSTALLED_APPS but then when I do manage.py runserver I get Error: no module named myapp
Does anyone know why?
Make sure you have two things:
correct path. You should be able to do ./manage.py shell and then import 'myapp' if not try 'myproject.myapp'
models.py in your app folder and __init__.py

Cannot start any django app

I am a newbie at Django and everytime I try to run
python panel/manage.py startapp %app% (panel is my project) it gives me the error:
Error: '%app%' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
Am I doing something wrong?
Surely companies or contacts or stats is not the name of an existing Python module?
This is a fun one - your project and your app need to have different names. You probably created a project, then tried to startapp with the same name.
I was confused as well, until I realized that a Django project is a container for applications; this sequence makes it a bit clearer:
# first create a Project (container).
django-admin.py startproject Project
# create multiple apps
cd Project
python manage.py startapp polls
python manage.py startapp hello
...
Perhaps you need to
cd panel
python manage.py startapp yourappname
I'm not sure running the command from a directory above your project will work properly.
I had the same issue because I was trying to "restart" my app after carrying out changes, but startapp is meant to be used once to create a new app. To view changes, syncronize app with DB with python manage.py migrate and restart the server with python manage.py runserver instead.
From the django-admin docs
(manage.py does essentially the same thing as django-admin)
startapp <app_label> [destination]
django-admin startapp
Creates a Django app directory structure for
the given app name in the current directory or the given destination.
By default the directory created contains a models.py file and other
app template files. (See the source for more details.) If only the app
name is given, the app directory will be created in the current
working directory.
If the optional destination is provided, Django will use that existing
directory rather than creating a new one. You can use ‘.’ to denote
the current working directory.
For example:
django-admin startapp myapp /Users/jezdez/Code/myapp
This message is displayed if you run "startapp" twice with the same app name. As pointed out above by the OP it doesn't reload the app, it creates one.
You should choose different names for your project and app in Codes:
django-admin startproject **my_project**
python manage.py startapp **my_app**
You need to create the directory before using the commands. Suppose you want a polls app inside apps folder.
mkdir apps apps/polls
python manage.py startapp polls apps/polls
I guess maybe you have already created the app's dir in panel dir manually. The command 'startapp' is to create an app automatically. If you already have one there, it fails.
I reproduced the issue and there's actually something not working as I expected.
I wonder if we stumbled upon a Django's bug, or a limitation that I don't understand.
Having a project called "project" and an empty folder app/newapp
…I tried:
python manage.py startapp newapp apps/newapp
It returns:
CommandError: 'newapp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
But if I target ANY other route in which the last folder is not called the same name as the app I'm starting, it works.
So I ended up doing:
python manage.py startapp newapp apps/main
Using Django 2.1.3.
if you want to make an empty directory that will contain your new app
project-dir
└── blog
├── __init__.py
├── ...
├── blog-ext #this empty dir that will contain the new app
└── views.py
so instead of typing :
python manage.py newapp blog/blog-ext
it should be :
django-admin startapp newapp blog/blog-ext
Try classic "mysite" or "myproject". You can delete it anytime you want, so if it will accepted, then all your privious ideas conflict with Python modules.
Edit: I tried all your ideas, there was no error for me. So, if you installed support libraries or modules for django, then some of them can contains such names.
this error is because of the name conflicts between the app name and project name.you had given same name for your app and project .your project and app need to be different name .if you had given the same name the above mentioned error will occur .
understand the difference between app and project
Projects vs. apps
What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.
first create the project.
then create the app.
NOTE: name for app and project should be different
first create a project with projectname
django-admin.py startproject Projectname .
Then create app with appname. (to create your app make sure you are in the same directory manage.py and type this command)
python manage.py startapp Appname
It's the process how I got my doubt clear.
First, I created a directory inside my project directory and put __init__.py, models.py, admin.py, apps.py & views.py.
Then I ran python manage.py runserver & It work well.
Then as suggested on that page I used startapp command. I got this error :
CommandError: 'ucportal' conflicts with the name of an existing Python
module and cannot be used as an app name. Please try another name.
After that I deleted that directory and ran startapp command with same name and it worked fine.
So 'startapp' command is to create an app automatically. If you already have one there, it fails.
Answer given by #DAG worked for me.
I ran into this issue while trying to set up a Wagtail project.
Before creating the app, I had created and activated a virtualenv (using virtualenvwrapper) with the same name: $APPNAME. When I then ran wagtail start $APPNAME, Django looks for naming conflics in the $PYTHONPATH which in this instance points to /Users/User/.virtualenvs.
Naturally, this results in a conflict as /Users/User/.virtualenvs/$APPNAME already exists.
None of these answers helped me. In the end I ended up creating an app with a different name and then just renaming the directory to the app name I wanted all along. Note that you also will need to change the class name in apps.py to match your app name.
Just Simply Use This command
for Django Project Creation
python -m django startproject name_of_django_Project
for Django App Creation
python -m django startapp App_name
I had the same issue when working with wagtail cms. I got this error even there is no such a created app. This occurs when there is an app already that has the same name you need to create inside the site-packages directory.
Once you get this error, you need to check the following directory,
C:\Users\{user}\AppData\Local\Programs\Python\Python38-32\Lib\site-packages
If there is a package with the name same you want to create then you need to remove that package. Also make sure to check that package is important or not before deleting.
The application directory should be created first.
Example: apps/practice
The command appears to be duplicated, but it is correct.
Example: python manage.py startapp practice ./apps1/practice