So I'm using django-hosts so as to have multiple domains for each app. But then I'm having an issue when trying to access the url index for each of the apps in my base.html
I'm trying to have something like this in my base.html file
<div>
<ul>
<li>Home</li>
<li>Laundry</li>
<li>Tailor</li>
</ul>
</div>
and this is my hosts.py file
from django_hosts import patterns, host
from django.conf import settings
host_patterns = patterns(
'',
host(r'www', 'artisan.home_urls', name='www'),
host(r'laundry', 'artisan.laundry_urls', name='laundry'),
host(r'tailor', 'artisan.tailor_urls', name='tailor'),
host(r'admin', settings.ROOT_URLCONF, name='admin')
)
And I have 3 files, namely home_urls.py, laundry_urls.py, and tailor_urls.py and each of them have the same format as the one below
# home_urls.py
from django.urls import path, include
app_name = 'home'
urlpatterns = [
path('', include('home.urls')),
]
# tailor_urls.py
from django.urls import path, include
app_name = 'tailor'
urlpatterns = [
path('', include('tailor.urls')),
]
# laundry_urls.py
from django.urls import path, include
app_name = 'laundry'
urlpatterns = [
path('', include('laundry.urls')),
]
But then, the problem is that only the namespacing for the home.urls is working. For the laundry and tailor urls, I keep getting a namespace error and I have no idea why. I was wondering if maybe I made a mistake in defining the hosts.py file, but I'm just not sure. Any help would be appreciated. If there's any other file you need to understand the question better, just ask. I'd be glad to provide one. Btw, I'm using Django 3.0 with Python 3.7 if that helps.
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
My root urls.py
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')), # new
]+static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
My pages app urls.py
from django.contrib import admin
from django.urls import path
from pages import views
urlpatterns = [
path('', views.home_page, name='home_page'),
path('<tab>', views.home,name='home'),
]
With this, I am able to access
127.0.0.1:8000/Home
127.0.0.1:8000/About
127.0.0.1:8000/Services
127.0.0.1:8000/Portfolio
All the tabs with url entry.
But, when I create an url entry in html template, {% url 'About' %}
getting NoReverseMatch
Very clever - I see you have 'short circuited' adding extra url patterns with your 'tab' path.
The issue is that django - specifically your templates - do not know what url 'About' refers to as you have not declared it as a name to any route in your url patterns.
i.e. something like:
path('/path/to/about', views.about_function, name='About')
What you can do in your template is hard code the path, for example:
<a href='/hardcode/this/path'> About </a>
Just note that if you change the 'About' page path, you'll need to replace it everywhere it's defined in any of your templates - plus possibly some other side effects.
I am trying to internationalize a Django dictionary application using the Django translation system. I have successfully translated most of the content on the site but I am having problems translating URL patterns, with the exception of the admin page.
My goal is to change the language of both content and URL based on prefix language code in the URL. For example:
www.example.com/en/dictionary/define // (content in English)
www.example.com/it/dizionario/definisci // (content in Italian)
www.example.com/fr/dictionnaire/définis // (content in French)
...
I checked the Django documentation on this (https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#translating-url-patterns) but found it quite vague and could not really understand it.
project-level urls.py:
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import gettext_lazy as _
from dictionary import views as dictionary_views
urlpatterns = [
path('admin/', admin.site.urls),
]
dictionary_patterns = ([
path('', dictionary_views.index, name='index'),
path(_('define/'), dictionary_views.define, name='define'),
path(_('define/<slug:headword_slug>'), dictionary_views.headword, name='headword'),
], 'dictionary')
urlpatterns += i18n_patterns(
path(_('dictionary/'), include(dictionary_patterns, namespace='dictionary')),
)
As for app-specific urls.py, I have no clue on what to do there. Should I import the dictionary_patterns variable from above and add it to the app's urlpatterns list? Should I delete the app's urls.py file altogether?
Can anybody help?
Just try to add import include in the urlpatterns like
In the project/urls.py
from django.conf.urls import include
path('en/dictionary/',include('app URL here with extension.url'),
path('it/dizionario/',include('app URL here with extension.url'),
path('fr/dictionnaire/',include('app URL here with extension.url'),
In the Appname/urls.py
urlpatterns = [
url(r'^define$/',views.name of view,name=''),
url(r'^definisci/$',views.name of view,name=''),
url(r'^définis/$',views.name of view,name=''),
Check this out:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
I have this code in one file called urls.py
from django.conf.urls import url
from django.contrib import admin
from myblog import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', urls),
]
Im trying to redirect to another file in a module called myblog which contains another file called urls.py with the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
my views file which contains post_list method is as follows:
from django.shortcuts import render
def post_list(request):
return render(request,'myblog/post_list.html', {})
The result is that i keep getting the following error message:
module 'myblog.urls' from 'path\to\python\file\urls.py' is not a callable or a dot-notation path
Can anyone please explain to me where i am getting it wrong. I am new to django and python and i am using a tutorial from DjangoGirls.com. They use Django 1.8 and i have Django 1.9 installed.
Looking at the tutorial, uou can see that you have forgotten to use include.
First you have to add the import
from django.conf.urls import include, url
Then change the url pattern to:
url(r'', include('blog.urls')),
Be careful that you are using the correct module name - you have myblog, but the tutorial has blog.
You need to include the urls.py to link them
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(urls, namespace='MyOtherapp')),
]
Note: I've also added a namespace here, its not required, it just helps when your using the url template tag.