I am completely new in Django and currently learning it from Udemy course.
First, I created my Django project and then created my application. I have inserted my application on settings.py in INSTALLED_APPS of project folder and then re run the the server as well.
Now, I have created a view in my application folder and want to use that in url.py of project folder. I could not able to import it as its showing that package not found.
Please see my program structure below:
I have written the code on pycharm IDE. My code in url.py is below
from django.contrib import admin
from django.urls import path
from firstApp import views
urlpatterns = [
path('^$', views.index, name='index'),
path('admin/', admin.site.urls),
]
Try changing path('^$', views.index, name='index') to path('^$', include('firstApp.urls'), namespace='index')
And you need to import include from django.urls
Related
My Django site isn't working properly. It's hard to explain but when I run my Django web server and go to http://127.0.0.1:8000/hello/ I see "Hello, World!" as expected. But when I go to http://127.0.0.1:8000/dashboard/ I see the same thing, when I should be seeing "Hello". There is to much code to put on stack overflow and it won't make sense so I made a GitHub repo https://github.com/Unidentified539/stackoverflow.
So to simplify your life
urlpatterns = [
path("",views.pomo,name="pomo"),
path("agenda/",views.agenda,name="agenda"),
path("notes/",views.notes,name="notes"),
]
this is your urls.py file in you app
just type this in your project urls.py so you don’t make a mess with paths and includes
urlpatterns = [
path('admin/', admin.site.urls),
path("",include('pomofocus.urls'))
]
this is just an example you need to enter your own paths and views
What's your code problem?
According to your code, you have two same paths for the home page, and when you enter http://127.0.0.1:8000/hello/ Django look at the project-level urls.py (in your code djangoProject1/urls.py) and knows that must go in app-level urls.py (in your code practice/urls.py) and in this file, you have two same paths, and Django uses the first one so you get write page because first one is related to hello view and when you type http://127.0.0.1:8000/dashboard/ Django again look at the project-level urls.py and knows that must go in app-level urls.py and in this file again use the first path and you get incorrect HTML file (because both paths are the same and Django always use the first one)
How to fix this?
in project-level url.py set both paths to '' and in app-level set your project-level paths. (in other words, replace paths in app-level and project-level
djangoProject1/urls.py:
import practice.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('practice.urls')),
]
practice/urls.py:
from django.urls import path
from practice import views
urlpatterns = [
path('hello/', views.hello_world, name='hello_world'),
path('dashboard/', views.dashboard, name='dashboard')
]
Your code should be like in
practice/urls.py
from django.urls import path
from practice import views
urlpatterns = [
path(" ", views.hello_world, name='hello_world'),
path("dashboard", views.dashboard, name='dashboard')
]
Project1 urls.py
import practice.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(" ", include('practice.urls')),
]
I am learning the Django Web Framework but just could not work my head around this logic.
Let's say we have a Django App that has a very big urls.py where all the patterns are stored here. How would one go about splitting this into a urls folder, wherein it contains multiple 'urls.py' files? And not only that, to have a nested directory within the urls folder as well.
Example project structure of what I am thinking of:
>main_base
>__init__.py
>asgi.py
>settings.py
>wsgi.py
>urls.py
>secondary_app
>admin.py
>apps.py
>models.py
>__init__.py
>views
>home.py
>idea.py
>project.py
>business_partners
>customers.py
>vendors.py
>urls
>__init__.py
>home.py
>idea.py
>project.py
>business_partners
>__init__.py
>customers.py
>vendors.py
in main_base/urls,
I would have the following:
from django.urls import path, include
urlpatterns = [
path('', include('secondary_app.urls')),
]
in secondary_app/urls/init.py,
I would expect to do the following:
from django.urls import path, include
urlpatterns = [
path('', include('secondary_app.urls.home')),
path('', include('secondary_app.urls.idea')),
path('', include('secondary_app.urls.project')),
path('', include('secondary_app.urls.business_partners')),
]
And lastly in secondary_app/urls/business_partners/init.py,
I would do the following:
from django.urls import path, include
urlpatterns = [
path('', include('secondary_app.urls.business_partners.customers')),
path('', include('secondary_app.urls.business_partners.vendors')),
]
I have tested it and it works all the way up till the secondary_app/urls directory level.
But once I go one more level down, it throws a reverse match not found error.
Example error:
NoReverseMatch at /
Reverse for 'bp-customers' with arguments '('',)' not found. 1 pattern(s) tried: ['bp/customers']
Any help is really really appreciated!
If you want your views in separate files do this (assuming CBV):
main_base/urls.py:
from django.urls import path, include
urlpatterns = [
path('', include('secondary_app.urls')),
]
secondary_app/urls.py:
from django.urls import path
from .views.home import HomeViewName
from .views.idea import IdeaViewName
urlpatterns = [
path('', HomeViewName.as_view(), name='home'),
path('idea/', IdeaViewName.as_view(), name='idea'),
### other views
]
This way your views will be in separate files and you will have routing to them
You should read official docs on django routing because it will answer your questions in greater detail.
When you create a django project with django-admin startproject myprojectand a app with django-admin startapp myapp the default django project layout is like:
-basedir/
--myproject
---asgi.py
---__init__.py
---__pycache_/
---settings.py
---urls.py
---wsgi.py
--myapp
---migrations/
---admin.py
---apps.py
---__init__.py
---models.py
---tests.py
---views.py
---manage.py
Base project directory and also every django app comes with urls.py file.There is a couple benefits for this structure but mainly keeps all related urls in it's own appdir to keep maintenance easier. Than you can include your apps urls in myproject/urls.py. For example :
in myproject/urls.py
from django.urls import path, include
urlpatterns = [
path('', include('myapp.urls')),
]
But if you would like to change the layout of a django project you can try
basedir/
--root_configuration/
--root_apps/
to keep your project files in one file and all the django apps in one file
I am trying to change django default homepage with my custom design, but it's not working. I Try a lot but still the same issue, I am not Understanding what is the issue, it's working perfect on my local server, But I am implementing Django app on Live server, Please let me know where I am mistaking.
here is my django default urls.py file...
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from django.conf import settings
from django.urls import path, include
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('mainadmin/admin/', admin.site.urls),
url(r'ckeditor/', include('ckeditor_uploader.urls')),
url(r'^myadmin/', include('myadmin.urls')),
url('', include('homepanel.urls')),
path('', views.index, name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
There are many ways you can change the admin panel of the your django website.
here is the article which will tell you how can you implement that
I am watching this video to learn more about Django https://www.youtube.com/watch?v=v7xjdXWZafY
my code is exactly like his however I am getting an import error. It says "no modules named urls"
This is my code:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('weather.urls')),
]
You do need to create a file called urls.py within your weather app. You can find further details on the documentation.
I am trying to develop a job portal in Django. As I am new to Django, I am not able to figure out why import error is getting displayed upon page hit, after deploying it on server. It was however working fine when I was running and testing in eclipse environment.
Here is the project tree structure for your reference. I know its quite long.
As you can see, MeraJob is the main project name and accounts, companies, MeraJob, students are applications inside it.
I deployed this structure and when I hit, I get this error.
I have ensured all the urls.py files have imported views.py using from views import * or simply import views. I can't figure out what the problem is, can someone help me out in this regard? Thanks in advance.
EDIT
Here is the my MeraJob/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from . import views
import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),url(r'^login/$', 'django.contrib.auth.views.login', name='login_view'),url(r'^password/reset/$', 'django.contrib.auth.views.password_change'),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT }),
url(r'^logout/$', logout_page),
url(r'^accounts/password/reset/$', 'django.contrib.auth.views.password_change'),
url(r'^password-changed/$', 'django.contrib.auth.views.password_change_done'),
url(r'^$', main_page),
url(r'^contact/$', contact_page),
url(r'', include('companies.urls')),
url(r'', include('miscellaneous.urls')),
url(r'', include('students.urls')),
)
Silly problem!
I missed to observe that the python file (views.py) didn't have read permissions for others!
Did a chmod appropriately and it worked! Thanks for other suggestions!!!
try from . import views
it would be easier if you posted your urls.py