Как использовать одно приложение в двух разных проектах в Django
'''I made testapp application in baseproject here I create urls.py in testapp and include in base project , Now I just copy paste the testapp in derivedproject with all urls.py file and views.py , when I add the testapp urls in derived project urls.py using include function it is showing error...
I want to inform you that I made two projects in same directory and manage.py is inside the projects
Now I am pasting the whole error.....'''
PS C:\Users\hp\Desktop\For Django\day2\derivedproject> python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner self.run()
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24,
in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 224, in create
import_module(entry)
File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'testapp'
введите код здесь
#derivedproject - файл settings.py установленные приложения INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'testapp', ]
#derivedprojects- urls.py
.from django.contrib import admin from django.urls import path,include
urlpatterns = [ path('admin/', admin.site.urls), path('testapp/',include('testapp.urls')) ]
#testapp-urls.py
from django.urls import path from . import views
здесь находятся урлы уровня приложения
urlpatterns = [ path('first/', views.first_view), path('second/', views.second_view), path('third/', views.third_view), путь('четвертый/', views.fourth_view), path('fifth/', views.fifth_view), ]
#derivedproject testapp views.py
from django.shortcuts import render from django.http import HttpResponse def first_view(request): return HttpResponse('<h1>first view response</h1>') def second_view(request): return HttpResponse('<h1>Second view response</h1>') def third_view(request): return HttpResponse('<h1>Third view response</h1>') def fourth_view(request): return HttpResponse('<h1>fourth view response</h1>') def fifth_view(request): return HttpResponse('<h1>fifth view response</h1>')