Django-admin collectstatic is an unknown command - django

Trying to collect my static files as I will be deploying my website soon. Receiving:
Unknown command: 'collectstatic'
After checking django-admin help I receive this warning:
(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.).
Here is my manage.py:
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'xwordsite.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)
I have tried running manage.py shell, which did not solve the issue.
Also I'm unsure if it matters but all my static files are in the project dir.
Also static settings:
STATIC_URL = '/static/'
STATIC_ROOT = "C:/Users/Joseph/Desktop/xwordsite/static"

As per the docs:
Just write python manage.py collectstatic and it will work.

In this page, at first says django-admin collectstatic but end of the section there is a code block that says manage.py collectstatic. At first it confused me. I did try to solve this error and tried to configure settings etc. which did not worked.
After all these effort, suddenly it hit me and i wrote manage.py collectstatic and it worked.

Related

How i can running django project?

I can run my django project only on my own computer. If i copy all files on another computer and try to run it, I get error.
Virtualenv is running successfully, but then i get this message "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?" I tried to install django on other computers too, but it isnt helps. What is wrong?
Project
.idea
__pycatch__
main
Project
templates
venv
db.sqlite3
manage
start
start.bat
#echo off
cmd /k "cd /d C:\Users\[user]\PycharmProjects\Project\venv\Scripts & activate.bat & cd /d C:\Users\[user]\PycharmProject\Project & python manage.py runserver"
manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Project.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)
if __name__ == '__main__':
main()
I could solve this problem. If somebody has same problem I replace "python manage.py runserver" with "py -m manage runserver"

No module named 'djangosite' when trying to use manage.py

I am using Django 3.0 and python 3.7 to run my Django server, it has only just stopped working and was working before.
The full error is here That was running python3 manage.py runserver and it is the same for any commands with manage.py
I have tried with and without my virtual environment and have reinstalled Django but it hasn't changed
the only place where djangosite is mentioned is in manage.py which I haven't changed
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangosite.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)
if __name__ == '__main__':
main()
Django is installed and I can run other sites
File tree is here
I had incorrect file structure. The settings.py was in the main folder with manage.py
It should have been in the mysite folder that has the main urls.py etc.
I fixed it by just re-creating the Django project and copying my apps and static file directory
You don't have Django installed or it is not available in your PYTHONPATH.
Perhaps you just did not activate your virtualenv in command shell.

ModuleNotFoundError: No module named 'django' while running manage.py

I have installed virtualenv and then installed django in my Windows 10. After activating virtualenv and running: python manage.py runserver, I am getting:
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
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?
Also just found while running django-admin.exe I am getting:
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.).
Manage.py:
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wordcount.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)
if __name__ == '__main__':
main()
You can verify whether you have installed django by creating a python program and importing django
import django
print (django.VERSION)
Above code will print the django version installed , confirm whether you have installed django
You have 2 Python versions : The main one which is installed by default and the one used by virtualenv.
When you run pip install django Django is installed in the main version of Python and thats because the PYTHONPATH environment variable refers to path of the main version and not virtualenv.
The same thing happens when you run python manage.py runserver. It doesn't run python from the virtualenv.
To solve this you need to access pip from the virtualenv and then you can insall Django with it
C:\the\path\to\virtualenv\path\to\pip.exe install django
And just like pip, run python.exe from virtualenv
C:\the\path\to\virtualenv\path\to\python.exe manage.py runserver
If you are using PyCharm for development then you can easily set your venv as interpreter.
Now to run django and just like pip you will access python from virtualenv
1 - File > Settings > Your Project > Python Interpreter
2 - Click the settings icon at the right and then click on Add
3 - Click on Virtualenv Environment and choose the location
Once the virtualenv has been setup you can easily use PyCharm to manage packages

Should I version control manage.py in Django?

Using this command:
django-admin.py startproject mysite
The following directory structure is generated:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Should I version control manage.py which contains:
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '[REDACTED].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)
Looking at the contents of the file, I don't think I need to, but just to be sure, I'm asking you lovely people :)
I think you should just add it to version control. You don't need to, but there's no reason you shouldn't version it.
Is it a part of your application: Yes, other developers need to use manage.py, so perhaps it should be left in version control so everyone is always using the same manage.py file.
Could manage.py change over time: Yes, while most people might not modify manage.py, it can be modified if you ever feel a need to so it should probably be versioned.
Does it contain any secrets or credentials: No, because there are no secrets it in, there's no reason not to version it.

Django not finding apps in virtualenv when using manage.py syncdb

My problem is in getting manage.py syncdb to run within a virtualenv.
It was working fine at one point, but seems to have broken sometime around when I installed South and updated pip and distribute.
Anyways, when the virtualenv is activated, I can import apps fine within the interactive interpreter. Running through mod_wsgi, the apps are imported as well, and the site can run.
When I run manage.py syncdb, it fails to find any app in INSTALLED_APPS that is in my virtualenv. It picks up system-installed apps fine, but fails when it tries to import virtualenv only apps.
Hi This is an old question, but saw its not answered. Not sure what you are attempting to do, but there are basically two modes you can use virtualenv,
For development, to create self-contained environments
For deployment, to create self-contained environments
In the first case, you need to first Activate your virtualenv with source venv/bin/activate, for when you deploy, you need to ensure that the virtualenv is activated for your website code. Personally i prefer the following approach to ensuring your path is set correctly. (I also add this to my manage.py when doing development, so i dont have to worry about activating the environment first.
Modified manage.py
#!/usr/bin/env python
import os.path
# Cater for Virtual env, add to sys.path
pwd = os.path.abspath(os.path.dirname(__file__))
project = os.path.basename(pwd)
new_path = pwd.strip(project)
activate_this = os.path.join(new_path,'venv','bin','activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)
This works, due to how i structure my projects, you would have to change it to your directory structure. My projects are structured like so:
TopLevelDir
|
|- Project DIR
|- venv
|- requirements
|- deployment configs
I have a simple solution to this
Just launch manage.py from the python in the bin of your virtual environment.
So say your python is here /home/tom/environments/my_env/bin/python you could launch manage.py like so:
/home/tom/environments/my_env/bin/python manage.py syncdb
then just create a symlink to the virtual environment's python inside your django project and call it env_python then you can do this:
./env_python manage.py syncdb