I'm new here in django python, right now I'm working with rest api, So I have created new app trialrisk, first i have added my app in settings.py file, After then when I am trying to add url in urls.py file I'm getting an error : ModuleNotFoundError: No module named 'trialrisk.urls' in python, Here I have added the whole code and my folder structure, Can anyone please look my code and help me to resolve this issue ?
Folder Structure
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'trialrisk',
]
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('/', include('trialrisk.urls'))
]
There is no urls.py file in trialrisk folder. Create the same in trialrisk folder and import from it.
In addition to adding urls.py to trialrisk, you'll also have to add a urlpatterns object to it e.g. urlpatterns = [], or you'll get an error like this
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'trialrisk.urls' from '/pathtoyourproject/trialrisk/urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.
Related
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),
]
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 am trying to create an webpage using Python and Django. I have just created a simple template and tried to run the sever but I get errors and I'm not able to understand..
I check all the spelling in my projects and searched the similar issues, however I am not able to solve it.
settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My apps
'learning_logs',
]
urls.py
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('learning_logs.urls')),
]
views.py
from django.shortcuts import render
# Create your views here.
def index(request):
"""The home page for Learning Log"""
return render(request, 'learning_logs/index.html')
urls.py // This is second URL
"""Defines url patterns for learning_logs."""
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = [
# Home page.
path('', views.index, name='index'),
]
Error on command prompt when I tried to run server:
"C:\Users\User\Desktop\python_work\project_3\learning_log\11_env\lib\site-packages\django\urls\resolvers.py", line 535, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
During handling of the above exception, another exception occurred:
.....
"C:\Users\User\Desktop\python_work\project_3\learning_log\11_env\lib\site-packages\django\urls\resolvers.py", line 542, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'learning_logs.urls' (namespace)>'
does not appear to have any patterns in it.
If you see valid patterns in the file then the issue is probably caused by a circular import.
You have created two paths in both urls.py for the / page.
As I know this is not the correct way.
You should make one path to /.
And make the second path by giving any path name like
path(learn/'', include('learning_logs.urls')
In your project urls.py, try changing like:
from django.urls import path, re_path, include
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include('learning_logs.urls')),
]
If you see valid patterns in the file then the issue is probably caused by a circular import.
So first you must check all installed package is installed in vurtual environment.
If you are in doubt,
Let's remove everything except from setuptools, pkg_resources, pip from site-packages.
and then
Install again in a virtual environment
$ python3 -m pip install -r requirements.txt
Recently I started my web project using Django with reference to some youtube tutorials. I have created a project named admin and created an app named user. From many tutorials they have created separate urls.py for both project and app. But in my case only the project's urls.py is working.
For example, I have defined the index view (my home page) in project's urls.py and login view in app's urls.py. When I try to access the login as it shows:
The current path, login, didn't match any of these
Can anyone help me to find out the solution for this?
Django only checks the URLs of the project's ROOT_URLCONF e.g. urls.py. If you want to include the URLs of a certain app, you can do the following:
In your projects urls.py:
from django.urls import include, path
urlpatterns = [
path('user/', include('user.urls')),
# others...
]
Then, if you go to url yoursite.com/user/, it will find the URLs specified in the app's urls.py. For more information see the docs.
Also make sure that your app user is in the INSTALLED_APPS of your settings.py, like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
...
'user',
...
]
So that django knows about your app.
I've set up Django on my Windows 10 PC, and was working through the first tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial01/
I can't seem to do the first part, because of an import error.
Here's the views.py script:
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
urls.py in the polls app:
mysite/polls/urls.py
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
And urls.py in mysite:
mysite/mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
After troubleshooting and fixing issues with the first two files, this is the error I get when I run "python urls.py" on the mysite urls.py file:
I've seen a few stackoverflow posts here regarding this tutorial, and similar issues. Someone advocated that I add polls to the INSTALLED_APPS section of settings.py, but this did not work.
/mysite/mysite/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
I also read that someone got this to work by adding the 'polls' module to their pythonpath... But I'm not sure if this is the way to go.
A very similar post was made here: Django reusable apps tutorial, ImportError: No module named 'polls'
but no solution was provided.
Anyone with some insight or input, please let me know what I can do to fix this, and let me know if you need any more information.
You seem to be running urls.py. shouldn't you be running python manage.py runserver command to start the app. Are you trying to achieve something else here ?