thanks for reading.
When I run "py manage.py makemigrations" I get the following message:
"ModuleNotFoundError: No module named 'transformaTe'"
This is the apps.py code:
from django.apps import AppConfig
class TransformateConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'transformate'
The name is updated there and in my INSTALLED_APPS:
INSTALLED_APPS = [
'transformate',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Where else should I look to change the name of the app?
This is the simplified structure of the app:
\my-app
\transformate
admin.py
apps.py
models.py
urls.py
views.py
\my-app
asgi.py
settigs.py
urls.py
wsgi.py
All this happened when I rename the app because I had a problem creating a table called transformaTe_myuser so I though all could be caused by the capitalized letter use.
Is there a better way of renaming an existing app in Django? I followed this steps, except for the migration part (Because I deleted the db and the migrations folder):
https://odwyer.software/blog/how-to-rename-an-existing-django-application
Thanks for your help.
Well, it turns out that when you run makemigrations you have to check the project urls.py file, that was the only thing that was crashing my migrations.
ORIGINAL my-app/urls. py FILE:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('transformaTe.urls')),
path('admin/', admin.site.urls),
]
NEEDED FILE:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('transformate.urls')), # Here was the bug
path('admin/', admin.site.urls),
]
Related
I tried to change setting to below but doesn't work:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp']
my directory:
manage.py
django_project
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
myapp
migration
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
it keeps showing ModuleNotFoundError: No module named 'myapp.urls'
views.py from myapp:
from django.shortcuts import render, Httpresponse
def index(request):
HttpResponse('Welcome!')
urls.py from myapp:
from django.urls import path
urlpatterns = [
path('', views.index ),
path('create/', views.index)
path('read/1/', views.index)
]
urls.py from django_project:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls'))
]
Thank you!
Try adding
in myapp/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myapp'
Maybe you need to set this ?
Docs
I'm new to django so take me easy. I'm just following some youtube tutorials and try to make a simple app to print Hello on the browser with django. And I keep getting this error in the urls.py file
ModuleNotFoundError: No module named 'app'
I get this error in urls.py file of my project which is bellow:
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('app.urls')),
path('admin/', admin.site.urls),
]
I also made an app called app
app\urls.py
from django.urls import path
import views
urlpatterns = [
path('', views.home, name = 'home'),
]
app\views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse('Hello!')
I read I think all threads on this topic and nothing helped so far and I can't realise where's my mistake or what I did wrong.
In your settings.py, add 'app.apps.AppConfig', in INSTALLED APP. You have to register the newly made apps to settings.py.
INSTALLED_APPS = [
'app.apps.AppConfig', // Added the name of app
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
]
*Note:- Everytime you add an app, register it in settings.py
I have a single Django project which has two different Django applications, MyApp1 and MyApp2 (changed their names).
The structure is like this:
MyProject
MyApp1
static
templates
urls.py
views.py
...
MyProject
settings.py
urls.py
...
I wrote the MyApp2 as a separate application in a different Django project and integrated it with the MyProject project (pip install MyApp2). Here is the installed apps in the settings.py file:
# Application definition
INSTALLED_APPS = [
'MyApp1',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'MyApp2',
]
These are the following paths (urls.py) of the MyProject.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp1/', include('myapp1.urls')),
path('', include('myapp1.urls')),
path('myapp2/',include('myapp2.urls')),
]
These are the following paths (urls.py) of the MyApp1:
from django.urls import path
from . import views
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
path('contact/', views.contact, name='contact'),
]
These are the following paths (urls.py) of the MyApp2:
from django.urls import path
from . import views
urlpatterns = [
path('', views.login, name='login'),
path('index', views.home, name='home'),
path('dashboard_output', views.dashboard_output, name='dashboard_output'),
path('login', views.login, name='login'),
]
On the browser, if I type http://127.0.0.1:8000/ or http://127.0.0.1:8000/myapp1 I get the view of the MyApp1.
On the browser, if I type http://127.0.0.1:8000/myapp2 I get the view of the MyApp2 (the login page).
So far so good, but if I type http://127.0.0.1:8000/myapp2/index the system directs me to the view of the myapp1 instead of the view of the myapp2. What am I doing wrong? Any ideas?
I finally found what is causing the problem. It has to do with templates. Check out this:
https://www.webforefront.com/django/customizedjangotemplates.html
It is really important how Django works. If you have multiple applications and if APP_DIRS is set to true ('APP_DIRS': True) in the settings.py file, it checks out all templates directories of each application in the order of the applications in the INSTALLED_APPS. Long story short, since I had index.html files in templates directories of both MyApp1 and MyApp2 applications, it was overriding the index.html file of the MyApp2's template directory with the index.html file of the MyApp1's template directory.
As a solution, I just added a subdirectory called myapp2 under the template directory in the MyApp2 application and included the subfolder name while rendering it.
MyProject
MyApp1
static
templates
index.html
...
urls.py
views.py
...
MyApp2
static
templates
myapp2
index.html
...
urls.py
views.py
...
MyProject
settings.py
urls.py
...
As I said, I added the subdirectory name (myapp2/) to the render function's second parameter. Here it is:
...
return render(request, "myapp2/index.html", result)
I could add a subdirectory to the templates folder in the MyApp1 too but just didn't prefer it for now, because it really got a big application and didn't want to mess up with it.
Anyway, the solution worked!
I am currently trying to complete the Django 2.0 tutorial Poll application. I am using Django version 2.0.7 and Python 3.7.0. I have set up a virtual environment for this project. I am very new to Django so apologies if my terminology is incorrect.
Basically, I cant get part 1 to work.
I have tried numerous times to run it and I cant seem to figure out why it wont work.
Before I create the Polls app the site runs correctly and I get the rocket ship, however after I create the app, update polls/views.py, create the polls/urls.py file and update the mysite/urls.py, I get the following error:
Page not found (404)
Request method GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/
admin/
The empty path didn't match any of these.
I thought my code was incorrect, so I copied and pasted the code from the website directly and tried to run it. It still wont work.
Below is the code I am trying and the directory layout. Any help would be greatly appreciated. I'm sure I am just missing something simple.
Thank you.
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'), ]
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite
print.txt
settings.py
urls.py
wsgi.py
init.py
pycache
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\polls
admin.py
apps.py
migrations
models.py
tests.py
urls.py
views.py
init.py
pycache
have you added your app name inside settings.py make sure you have added the name in settings.py which resides under project folder
in your case it will be here:
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite
like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',] # <--- polls in your case
make sure you are using your routes you have defined to access the correct page
for example in your case:
your website's routes are:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
and your application 'polls' routes are:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
according to your views.py file
which is this:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
you must visit http://localhost:{port number}/polls/ or http://127.0.0.1:{port number}/polls/
default port number is 8000
after running server using python manage.py runserver
to get your page as an output on browser
I have issues importing my app in url patterns, but everything seems in place. I get errors in from fairy_app import views and an error name 'News' is not defined
directory:
fairy-
fairy-
_init_.py
settings.py
urls.py
wsgi.py
fairy_app-
_init_.py
admin.py
models.py
tests.py
views.py
db.fairy
manage.py
views.py
from django.shortcuts import render
from django.views.generic import TemplateView
# Create your views here.
class News(TemplateView):
template_name = 'news.html'
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from fairy_app.views import News
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'fairy.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', News.as_view()),
)
setting.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fairy_app',
)
In your urls.py you reference the News view without importing it. You import the views file as module.
So you can either do:
views.News.as_view()
or:
from fairy_app.views import News
2nd way is shorter but gets inconvenient if you have many view classes, so I prefer the first one.
I found the same problem,but got sorted it out.
Create a new python file in app-level for urls
use include function in fairy-/urls file to include all other urls of the app and import views in the new file
for example;
#in url.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^greet/',include('greetings.url')),
]
#created a new file(url.py) in greetings app
#in greetings/url.py
from . import views
urlpatterns = [
url(r'^$',views.greet),
]
#greet is the function in my views`
I had the same problem. I worked around it by giving the full path
from fairy.fairy_app.views
alternatively
from ..fairy_app.views