Can't use reverse url in django project - python

I have built a django project, and the folders are like this
project_dir
django_dir
settings.py
urls.py
app_dir
urls.py
tables.py
views.py
And in tables.py I have added:
from django.core.urlresolvers import reverse
print reverse('caseUpdate',args=[1])
system raised:
The included urlconf django_dir.urls doesn't have any patterns in it
BUT I used reverse in django shell, it works !
Where can I start to debug this ?
print out my urls.py
django_dir --> urls.py as below:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django_dir import settings
from django.conf.urls.static import static
# xadmin, powerful and elegent admin system written by chinese
import xadmin
from xadmin.plugins import xversion
xversion.register_models()
admin.autodiscover()
xadmin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^static/(?P<path>.*?)$','django.views.static.serve',{
'document_root':settings.STATIC_ROOT}),
url(r'^admin/', include(admin.site.urls)),
url(r'^dispute/',include('dispute.urls')),
url(r'xadmin/', include(xadmin.site.urls)),
)
app_dir --> urls.py as below:
from django.conf.urls import patterns, include, url
from app_dir import views
urlpatterns = patterns('',
url(r'^$',views.dashboard,name='dashboard'),
url(r'^(?P<caseId>\d+)refund/',views.refund,name='refund'),
url(r'^(?P<caseId>\d+)reply/',views.reply,name='reply'),
url(r'^(?P<caseId>\d+)/update',views.caseUpdate,name='caseUpdate'),
url(r'^(?P<caseId>\d+)/statuUpdate',views.statuUpdate,name='statuUpdate'),
url(r'^(?P<caseId>\d+)/performanceUpdate',views.performanceUpdate,name='performanceUpdate'),
url(r'^(?P<caseId>\d+)/',views.detail,name='detail'),
url(r'^search/',views.dashboard,name='case_search'),
)

Related

Using the URLconf defined in products.urls, Django tried these URL patterns, in this order:

*from django.urls import path, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('products/',include('products.urls')),
path('accounts/',include('accounts.urls')),
]*
The above code of pyshop/url.py
I could not login to admin page.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('products/new',views.new),
path('products/',views.products),
path('accounts/register',views.register),
]
The code is of product/urls.py.
Old question, but I run into the same problem, so just in case I can help someone else.
I would put in the pyshop/url.py, this code (see the admin path is the only one with an actual url):
from django.urls import path, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('products.urls')),
path('',include('accounts.urls')),
]
Then in your product/urls.py (See all of the routes end with a slash):
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="product"),
path('products/new/',views.new, name="new_product"),
path('products/',views.products, name="products"),
path('accounts/register/',views.register, name="register"),
]

url, path in urlpatterns Django is not working

path('',include('personal.urls')),
^
SyntaxError: invalid syntax
When I run the server using python manage.py runserver for the following code I got this error.
url patterns
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
from django.urls import path
#from django.conf.urls import url
#from django.urls import path
urlpatterns = [
url('admin/', admin.site.urls)
path('',include('personal.urls')),
]
url patterns for my app 'personal'
from django.urls import include
from django.conf.urls import url
from django.urls import path
from . import views
#import URLs
urlpatterns = [
path('', views.index, name='index'),
]
This is views.py code
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'personal/home.html')
It's the comma after the first line. Replace with this:
urlpatterns = [
url('admin/', admin.site.urls),
path('',include('personal.urls')),
]
It might be due to missing namespace. Try to include the namespace in your main.urls.
path('', include('personal.urls', namespace="personal")),

Updating protected serve url path with patterns for django 1.11

How do you use this code in the current (1.11) version of Django? The code below uses patterns module but as I googled it was removed in 1.10.
from django.conf.urls import patterns, include, url
from django.contrib.auth.decorators import login_required
from django.views.static import serve
from django.conf import settings
#login_required
def protected_serve(request, path, document_root=None, show_indexes=False):
return serve(request, path, document_root, show_indexes)
urlpatterns = patterns('',
url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], protected_serve, {'document_root': settings.MEDIA_ROOT}),
)
Here's the source tutorial link
In Django 1.8+, urlpatterns should be a list (release notes). Remove the patterns import as well.
from django.conf.urls import include, url
urlpatterns = [
url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], protected_serve, {'document_root': settings.MEDIA_ROOT}),
]

strange 404 error in djangocms when using details view

I have got an 404 error with djangocms, Raised by: cms.views.details.
I am trying to insert an external app in django CMS. When I run the app separately I dont have the 404 error on my detail view, all is working fine. But when I put my apps in djangocms, please note that the listview works fine and the detail view makes the 404 error.
I don't know what I'm doing wrong.
djangocms version 3.2
django 1.9
python 3.4
here the url.py of my external app
from django.conf.urls import patterns, url
from . import views
from .views import DocListView, DocDetailView
app_name = 'inventaire'
urlpatterns = patterns('',
url(r'^document/(?P<pk>[0-9]+)/$', views.DocDetailView.as_view(), name='detail'),
url(r'^document$', views.DocListView.as_view(), name='index'),
)
Here the views
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Document, Mention
# Create your views here.
class DocListView(generic.ListView):
template_name = 'inventaire/index.html'
context_object_name = 'latest_document_list'
def get_queryset(self):
"""Return the last five published questions."""
return Document.objects.all
class DocDetailView(generic.DetailView):
model = Document
template_name = 'inventaire/detail.html'
here my url.py from my cms
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
from cms.sitemaps import CMSSitemap
from django.conf import settings
from django.conf.urls import * # NOQA
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
{'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^select2/', include('django_select2.urls')),
url(r'^', include('cms.urls')),
url(r'^inventaire/', include('testTemplates.apps.inventaire.urls')),
)
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + staticfiles_urlpatterns() + urlpatterns # NOQA
The 'cms.urls' include must be the last in your urlpatterns. It will catch all requests, so move your 'inventaire/' include above that.

Django url Routing Error

I have a very basic url router in my django project:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = staticfiles_urlpatterns()
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^/?', include('customApp.urls')),
)
When I start the dev server and go to 127.0.0.1:8000/admin/, I get a ViewDoesNotExist at /admin/ error.
Here is the content of the exception:
Could not import customApp.views.event. View does not exist in module customApp.views.
I've already tried re-ordering the urls (I have no idea how that would help, but I tried it anyway) and changing r'^/?' to r'^/'.
When I comment out the last url, the admin page works again.
Here's the customApp.urls code:
from django.conf.urls import patterns, include, url
import django.contrib.auth.views
import django.contrib.auth
urlpatterns = patterns('customApp.views',
url(r'^$', 'index'),
url(r'^rest/v1/event/add/$', 'event'),
url(r'^rest/v1/reports/$', 'reports'),
)
urlpatterns += patterns('',
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
)
It is simple buddy. Django cannot find customApp views. Please ensure whatever views you have got in urls.py, it should exist.
Thanks

Categories