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.
Related
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 uploaded my site to the live server, but still, I am seeing Django default page, I updated my urls.py file, but still, it's not working. and it's showing me this output. Please let me know where I am Mistaking.
I am trying to access this mydomain.com/dashboard
Using the URLconf defined in yoursite.urls, Django tried these URL patterns, in this order:
admin/
The current path, dashboard, didn't match any of these.
here is my urls.py file...
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^dashboard/', include('dashboard.urls')),
url(r'^accounts/', include('accounts.urls')),
url('', include('frontpanel.urls')),
path('', views.index, name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here is my views.py file...
def index(request):
category = Category.objects.all().order_by('created_at')
return render(request, "base.html", {'category':category})
What version of Django are you using?
Try changing url to re_path (remember to import it first). I think url has been deprecated.
After Analyzing the question u never mentioned ur app urls.py you only mentioned project urls.py . As u have url(r'^dashboard/', include('dashboard.urls')), in ur project urls.py, there must be a file in your app also named urls.py which handles urls having prefix /dashboard/ , by default django doesnt make that file you need to manually make it add ur function names to redirect . For example this is my main urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('display_report/', include("display_report.urls"))
]
then u have to make a file named urls.py in ur app also to handle the requests and redirect the the approaite functions , in my display_report app i made a urls.py that looks like this
from django.contrib import admin
from django.urls import path, include
from display_report import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [path('', views.index)]
And then it will redirect to function named index in ur views.py file inside ur app
from django.shortcuts import render, redirect, HttpResponse
# from .models import Employee, Tasks, Report, Fileupload, Fileuploadnext
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render(request."index.html")
Here my ur will be mydomain.com/display_report and my index.html file will be inside the template folder
i'm building my first Django app so i'm know i'm missing a lot of things, but i installed a gallery for my site and is inside site-packages, i set the url inside urls.py and it looks like this :
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from contacto.views import Home,Contacto
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls'),name='gallery'),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I can perfectly access the gallery and its functionalities if i write the url in the browser, but i cannot reference the gallery Url inside my template like the other Urls using {% url 'gallery' %}, i keep getting this error :
Reverse for 'gallery' not found. 'gallery' is not a valid view function or pattern name.
Also heres the gallery url:
from django.urls import path
from gallery.views import ImageView, ImageList, AlbumView, AlbumList, ImageCreate
app_name = 'gallery'
urlpatterns = [
path('', AlbumList.as_view(), name='album_list'),
path('images/', ImageList.as_view(), name='image_list'),
path('image/<int:pk>/<slug>', ImageView.as_view(), name='image_detail'),
path('upload/', ImageCreate.as_view(), name='image_upload'),
path('album/<int:pk>/<slug>/', AlbumView.as_view(), name='album_detail'),
path('album/<int:apk>/<int:pk>/<slug>', ImageView.as_view(), name='album_image_detail')
]
Thanks!
app_name = 'gallery'
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls')),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
Change it as shown above and call gallery:album_list for album list, and follow same pattern for others
register.html is not accessible on 'register/' url
tried changing url to '' and webpage is accessible but not on 'register/'
projects urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('login/',include('login.urls')),
path('admin/', admin.site.urls),
]
apps urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.indexView, name = "home"),
path('dashboard/', views.dashboardView, name="dashboard"),
#path('login/',),
path('register/',views.registerView, name="register_url"),
#path('logout/',),
]
views.py
from django.shortcuts import render
def indexView(request):
return render(request,'index.html')
def dashboardView(request):
return render(request,'dashboard.html')
def registerView(request):
return render(request,'register.html')
templates/register.html
Either you type login/ before evry URL pattern of apps urls.py file while testing URL.
or
You should include url mapping of apps urls.py in main urls.py
This might help you.
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('apps.urls')), #put you app name here
path('admin/', admin.site.urls),
]
apps urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.indexView, name = "home"),
path('dashboard/', views.dashboardView, name="dashboard"),
path('register/',views.registerView, name="register_url"),
]
This will work and you can add login/ url mapping inside the apps urls.py.
This type of flow will make it easy to understand .
Use url_name for URL calling:
{% url 'register_url' %}
This will directly search for 'register_url' in the project and will return the path.
url_name must be unique so make sure your url_name is uniquely defined.
I have the following http address when running in development (manage.py runserver)
http://127.0.0.1:8000/
When using wamp i use an alias named picon
http://localhost/picon/
When clicking on links from the nav bar the navigation works fine for both deployments. But when i click from the following views.py the urls break;
def addcustomer(request):
form = AddCustomerForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Customer added succesfully')
return HttpResponseRedirect('/picon/customers')
return render_to_response("addcustomer.html",
locals(),
context_instance=RequestContext(request))
In the production server i use the prefix /picon/ because otherwise the link can not be found when using an alias. Off course this alias is not available on the development version.
So my question, can i create a dynamic alias that has a relation to the debug status in the settings.py file.
for example;
If Debug:
alias = ''
else:
alias = 'picon/'
if this is possible, how would i then reference to the alias in the views.py using the following line;
return HttpResponseRedirect('/picon/customers')
Edit: added my urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'signups.views.home', name='home'),
url(r'^thank-you/$', 'signups.views.thankyou', name='thankyou'),
url(r'^about-us/$', 'signups.views.aboutus', name='aboutus'),
url(r'^customers/$', 'formlist.views.customers', name='customers'),
url(r'^addcustomer/$', 'formlist.views.addcustomer', name='addcustomer'),
#url(r'^addcustomer_res/$', 'formlist.views.addcustomer', name='addcustomer'),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Any suggestions are much appreciated.
Regards.
This is exactly why you should not hard-code URLs in views or templates. Use django.core.urlresolvers.reverse - or the {% url %} template tag - to calculate the URL dynamically, including any prefix.
return HttpResponseRedirect(reverse('customers'))
where 'customers' is the name value from the URLconf.
You can include the urls from another file, dynamically:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
#PAY ATTENTION HERE
from another_location import urls as another_urls
admin.autodiscover()
if settings.DEBUG:
urlpatterns = url(r'^picon/', include(another_urls))
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
else:
urlpatterns = url(r'^', include(another_urls))
Having your another_location.urls file like this:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'signups.views.home', name='home'),
url(r'^thank-you/$', 'signups.views.thankyou', name='thankyou'),
url(r'^about-us/$', 'signups.views.aboutus', name='aboutus'),
url(r'^customers/$', 'formlist.views.customers', name='customers'),
url(r'^addcustomer/$', 'formlist.views.addcustomer', name='addcustomer'),
#url(r'^addcustomer_res/$', 'formlist.views.addcustomer', name='addcustomer'),
url(r'^admin/', include(admin.site.urls)),
)
You must ensure such file is pythonpath-reachable via imports.
Edit: also, don't hardcode URLS. Always use reverse. It will be harmless to you since you gave names for each of your urls (reverse('customers') will give you the right url no matter where is it deployed).