Cannot start any django app - django

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

Related

Do you think this is the right way to rename a django app?

I read different threads and tutorials on renaming and migrating a Django app (How to rename a Django app and migrate data from an app to the other for instance), I also tried to use the django package called django-rename-app but it didn't work at all.
I'm going to explain the way I renamed my app in my development environment and I would like to know if you expect troubles, as apparently everything is working just as it did before so I'm considering doing the same in production.
Here are the steps I took inside my root project directory and my virtualenv activated, using Django 3.2:
Create the destination app
django-admin startapp app2
Copy these files and directories
cp app1/*.py app2/
mkdir app2/templates
mkdir app2/static
cp app1/templates app2/templates
in this case I had a sub-folder named after the app so I renamed it like this
mv app2/templates/app1 app2/templates/app2
cp app1/static app2/static
Models.py
Change the related_name attributes of all ForeignKeys in my new models.py and update all their references inside my views, admin, form and templates
Make sure your apps won't imported by the same name
Look for the instanciation of an AppConfig class inside your app directory and if you find something like this:
from django.apps import AppConfig
class AnunciosConfig(AppConfig):
name = 'app1'
that's where you need to also change your app name from app1 to app2
Activate the new app
Add my app2 to the list of the enabled apps of settings.py
Delete __pycache__
Find and delete or empty all the __pycache__ directories in app2
Migrations
this will not work until all the overlapping bits are corrected, so this is how I tested everything was ok (it will not be aware of URLs problem at this stage)
python manage.py makemigrations
python manage.py migrate
Cloning data
Now I used the Django shell to migrate data from app1 to the app2
python manage.py shell
import app1
from app2.models import *
So I will have to use "app1.ModelName" to refer to the old app and simply "ModelName" to address the new app2 instances
Then I use the Models and QuerySet API to just create new objects in app2 with for loops for each app1 instances, with a corresponding app2 object that is saved (this needs be written individually and depends on each situation)
Deactivate the old app
Now I remove or comment out the old app1 reference in settings.py so that it is disabled
Rename the directory
At this point I needed to rename the app1/ directory to something unknown to Django
mv app1 app1_
Deal with the new errors
It seems some more errors appeared here with URLS and Views, so I had to just update app1 references to app2 until they disappeared
Delete Database table
Enter MySQL (in my case), and disable FOREIGN_KEY_CHECKS
USE yourowndjangodatabase;
SHOW TABLES;
SET FOREIGN_KEY_CHECKS=0;
so now I can delete them like this
DROP TABLE app1__yourfirstmodel;
DROP TABLE app1__yoursecondmodel;
and so on until none is left, then set the FOREIGN_KEY_CHECKS back to 1 so that it is enabled
SET FOREIGN_KEY_CHECKS=1;
Like I saw here Can't drop table: A foreign key constraint fails
CONCLUSION
And basically that was it, it worked perfectly for my development environment. I have a perfect copy of my previous app1 and web application that is just using app2 now.
I improvised so I'm not sure this is the right procedure but it worked so I would like to ask if you see any problem or suspect there will be issues in production.

How to add site models to Cookiecutter Django project

I have a Cookiecutter Django project in which I'd like to add a "SiteSettings" model to control constants. My first instinct was to run manage.py startapp site. However I received the following message:
CommandError: 'site' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
Upon closer inspection, it appears that Cookiecutter already created a sites module within contrib, but with no models.py file. So I decided to make a models.py with the SiteSettings model in this directory.
However when I run makemigrations it says: No changes detected in app 'sites'
How can I add my desired model to this module and get migrations working for this module?
You will run into countless problems if you follow this path. Just use another name and do manage.py startapp newname.

Creating migrations for a reusable Django app

I am writing a reusable Django app and having problems creating migrations.
I have looked at this question, and I'm still confused. I have the following sort of directory structure:
django-mycleverapp/
django-mycleverapp/django_mycleverapp/
django-mycleverapp/django_mycleverapp/__init__.py
django-mycleverapp/django_mycleverapp/apps.py
django-mycleverapp/django_mycleverapp/models.py
django-mycleverapp/django_mycleverapp/urls.py
django-mycleverapp/django_mycleverapp/views.py
django-mycleverapp/example/
django-mycleverapp/example/manage.py
django-mycleverapp/example/example/
django-mycleverapp/example/example/__init__.py
django-mycleverapp/example/example/settings.py
django-mycleverapp/example/example/urls.py
django-mycleverapp/setup.py
As you can see, the directory "django_mycleverapp" contains my reusable app and the directory "example" contains a test project.
I include the models of "django_mycleverapp" in the INSTALLED_APPS section of the settings for "example".
However, running python ~/example/manage.py makemigrations django_mycleverapp doesn't build any migrations.
Any suggestions?
How am I meant to have a test project that will build migrations in "/django-mycleverapp/django_mycleverapp/migrations"?
Your app should be in the directory of your project. Your directory hierarchy should look like this.
django-mycleverapp/
django-mycleverapp/example/
django-mycleverapp/example/django_mycleverapp/
django-mycleverapp/example/django_mycleverapp/__init__.py
django-mycleverapp/example/django_mycleverapp/apps.py
django-mycleverapp/example/django_mycleverapp/models.py
django-mycleverapp/example/django_mycleverapp/urls.py
django-mycleverapp/example/django_mycleverapp/views.py
django-mycleverapp/example/manage.py
django-mycleverapp/example/example/
django-mycleverapp/example/example/__init__.py
django-mycleverapp/example/example/settings.py
django-mycleverapp/example/example/urls.py
django-mycleverapp/example/setup.py
If you do not want your app to be part of your "example" project, but rather want it to be separated and used by your project "example", you'll have to install it in your project using pip (this requires to have a setup.py at the root of your app).
For instance if you have published your app on a git repository "https://gitlab.com/myuser/myproject.git", you can add to our requirements.txt:
git+https://gitlab.com/myuser/myproject.git#v1.0#egg=myapp_name
If you don't have your app published on a git repository yet, you can add the absolute path to your app to you requirements.txt:
/path/to/django-mycleverapp/django_mycleverapp/
Don't forget to work in a virtualenv when you use pip.

Django App not working: "Error: No module named app_name"

I'm new to Django, and I can't figure out why my app isn't loading. I've created all the files: models.py, views.py, urs.py with default code in. But when I run python manage.py runserver, it says the following:
Error: No module named app_name
What am I doing wrong?
Did you remember to include __init__.py in your folder?
If you did, make sure the permissions on the file are also correct. There's also a similar question here.
Just an additional hint: Instead of manually creating the files you can use django-admin.py startapp APPNAME to automatically create a directory with all necessary files for a new app.
I got your point the init file thing also dosen't work out for me as well. Just ensure that are you writing the proper command inside the proper directory while creating the app, using the terminal.
Like while writing the command "django-admin startapp APP_NAME", ensure that the command is written inside your root configuration directory (which gets created after you typein command "django-admin startproject PROJECT-NAME"), not anywhere else. Then mention the app name inside the settings.py file, under the INSTALLED_APPS[ ] list. Then finally run the command "python manage.py runserver" in the same root configuration directory. I assure you, this will work for sure and the "No modules found" thing will disappear. Have a try and tell me if it don't. Thank you.

How do I create sub-applications in Django?

I'm a Django newbie, but fairly experienced at programming. I have a set of related applications that I'd like to group into a sub-application but can not figure out how to get manage.py to do this for me.
Ideally I'll end up with a structure like:
project/
app/
subapp1/
subapp2/
I've tried manage.py startapp app.subapp1 and manage.py startapp app/subapp1
but this tells me that / and . are invalid characters for app names.
I've tried changing into the app directory and running ../manage.py subapp1 but that makes supapp1 at the top level. NOTE, I'm not trying to directly make a stand-alone application. I'm trying to do all this from within a project.
You can still do this :
cd app
django-admin startapp subapp1
This will work (create the application basic structure), however app and subapp1 will still be considered as two unrelated applications in the sense that you have to add both of them to INSTALLED_APPS in your settings.
Does this answer your question ? Otherwise you should tell more about what you are trying to do.
According to Django documentation,
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
So, you can do it by this method:
Create sub_app1 directory in app directory
python manage.py startapp sub_app1 app/sub_app1
Django doesn't support "subapplications" per se. If you want code to be collected in packages within an app then just create them yourself. Otherwise you're just asking for pain.
Its Simple
Step 1 - Create Project
django-admin startproject app
cd app
Step 2 - Create api folder
mkdir api
cd api
touch __init__.py
cd ..
Step 3 - Create nested apps
python manage.py startapp user ./api/user
python manage.py startapp post ./api/post
python manage.py startapp comment ./api/comment
Step 4 - Register nested apps
INSTALLED_APPS = [
...
'api.user',
'api.post',
'api.comment',
]
Step 5 - Change name of Apps
Update the name in apps.py files in all three apps
class UserConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.user'
class PostConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.post'
class CommentConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.comment'
and We are done!
Note : Step 2 is important
Go to your apps folder. Try:
python ../manage.py startapp app_name
django-admin.py startapp myapp /Users/jezdez/Code/myapp
Reference: Django Admin documentation
In order to create a nested app in the Django project, you have to follow the following steps:
Create a subdirectory in the parent directory where you want to add a sub-app.
mkdir finances/expences
N.B: Here finances is the existing parent app and expenses will be the next nested sub-app.
If the subdirectory is created successfully then run the following command in the terminal.
python manage.py startapp expenses finances/expenses
Please check the expenses subdirectory is now looks like a Django app and will behave like that.
Don't forget to register this app in the settings file and change the name from the config file of the app expenses, like -
class ExpensesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'finances.expenses'
Here, the name was only 'expenses' but it has been changed to 'finances.expenses' otherwise no migrations will be applied.
Use this command in the terminal:
python manage.py startapp application_name
there is really no any differ if you use django-admin or manage.py in this case - both will create
https://docs.djangoproject.com/en/4.0/ref/django-admin/#django-admin-and-manage-py
In addition, manage.py is automatically created in each Django project. It does the same thing as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.