there is a problem with a url i've created in django that it doesn't totally work
this is urls.py
from django.conf.urls import include, url
from django.contrib import admin
from pizza import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('', views.home,name='home'),
url('order/', views.order,name='order'),
]
and this is views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Home page")
def order(request):
return HttpResponse("Order a pizza page")
That is incorrect syntax, try using path('') instead of url
If pizza it's your app you must use:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('pizza/', include('pizza.urls')),
]
in urls.py and add your app in settings.py inside INSTALLED_APPS
OR
If when you call from pizza import views is the name of Project you should import as import .views only
Update in project urls.py file.
I want to run test in Django, and I have a very simple test case that aims to test whether an url is reachable. I am using a valid path, but I still get a Resolver404 error. I don't know if it has something to do with multilanguage settings, or something.
This is my main urls.py:
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
from django.urls import path, include
from rest_framework.authtoken import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = i18n_patterns(
path('api-token-auth/', views.obtain_auth_token),
path('', include('FrontEndApp.urls')),
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('rosetta/', include('rosetta.urls')),
path('general/', include('GeneralApp.urls')),
#path('invoices_manager/', include('InvoicesManagerApp.urls')),
path('operations_manager/', include('OperationsManagerApp.urls')),
#path('payments_manager/', include('PaymentsManagerApp.urls')),
#path('providers_manager/', include('ProvidersManagerApp.urls')),
path('rates_manager/', include('RatesManagerApp.urls')),
#path('reports_manager/', include('ReportsManagerApp.urls')),
path('reservations_manager/', include('ReservationsManagerApp.urls')),
path('users_manager/', include('UsersManagerApp.urls'))
)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my app urls.py:
from django.urls import path, include
from GeneralApp import views, models
from rest_framework import routers, renderers
APP_NAME = 'GeneralApp'
urlpatterns = [
path('', include(router.urls)),
path('catalogs/', views.CatalogViewSet.as_view({'get':'list'})),
path('countries/', countries_list, name='rest_countries_list'),
path('countries/<int:pk>/', country_detail, name='rest_country_detail'),
path('countries/<int:pk>/states/', states_list, name='rest_country_states_list'),
path('states/', states_list, name='rest_states_list'),
path('states/<int:pk>/', state_detail, name='rest_state_detail'),
path('states/<int:pk>/cities/', cities_list, name='rest_state_cities_list'),
path('cities/', cities_list, name='rest_cities_list'),
path('cities/<int:pk>/', city_detail, name='rest_city_detail'),
path('cities/<int:pk_city>/zones/', city_zones_list, name='rest_city_zones_list'),
path('zones/<int:pk>/', zone_detail, name='rest_zone_detail'),
path('zones/', zones_list, name='rest_zones_list'),
path('airports/<int:pk_city>/airports/', city_airports_list, name='rest_city_airports_list'),
path('airports/', airports_list, name='rest_airports_list'),
path('airports/<int:pk>/', airport_detail, name='rest_airport_detail'),
path('languages/', languages_list, name='rest_languages_list'),
path('languages/<int:pk>/', language_detail, name='rest_language_detail'),
path('provider_types/', provider_types_list, name='rest_provider_types_list'),
path('provider_types/<int:pk>/', provider_type_detail, name='rest_provider_type_detail'),
path('companies/', companies_list, name='rest_companies_list'),
path('companies/<int:pk>/', company_detail, name='rest_company_detail'),
path('companies/<int:pk>/provider/', provider_detail, name='rest_provider_detail'),
path('providers/', providers_list, name='rest_providers_list'),
path('providers/<int:pk>/', provider_detail, name='rest_provider_detail'),
path('company_providers/', company_providers_list, name='rest_company_providers_list'),
path('company_providers/<int:pk>/', company_provider_detail, name='rest_company_provider_detail'),
path('contacts_hlkd/', contacts_hlkd_list, name='rest_contacts_hlkd_list'),
path('contacts_hlkd/<int:pk>/', contact_hlkd_detail, name='rest_contact_hlkd_detail'),
path('contacts/', contacts_list, name='rest_contacts_list'),
path('contacts/<int:pk>/', contact_detail, name='rest_contact_detail'),
path('exchange_rates/', exchange_rates_list, name='rest_exchange_rates_list'),
path('exchange_rates/<int:pk>/', exchange_rate_detail, name='rest_exchage_rate_detail'),
path('zipcodes/', zipcodes_list, name='rest_zipcodes_list'),
path('zipcodes/<int:pk>/', zipcode_detail, name='rest_zipcode_detail'),
path('zipcodes/<int:pk>/cities/', cities_list, name='rest_cities_list'),
path('zipcodes/<int:pk>/states/', states_list, name='rest_states_list'),
path('zipcodes/<int:pk>/contries/', countries_list, name='rest_countries_list'),
path('markets/', markets_list, name='rest_markets_list'),
path('markets/<int:pk>/', market_detail, name='rest_market_detail'),
path('parameters/', parameters_list, name='rest_parameters_list'),
path('parameters/<int:pk>/', parameter_detail, name='rest_parameter_detail'),
]
And this is my tests.py:
from django.test import TestCase
from django.urls import resolve
from GeneralApp.views import CountryViewSet
# Create your tests here.
class CountryEndpointTest(TestCase):
def test_url_resolves_to_view(self):
found = resolve('/general/countries/')
self.assertEqual(found.func, views.CountryViewSet)
And this is the error I get:
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/
urls/resolvers.py", line 523, in resolve
raise Resolver404({'tried': tried, 'path': new_path})
django.urls.exceptions.Resolver404: {'tried': [[<URLResolver <URLPattern list> (None:None) 'en-us/'>]], 'path': 'ge
neral/countries/'}
Pass prefix_default_language=False to the i18n_patterns function. If not, you must use the language code in all url patterns.
https://docs.djangoproject.com/en/2.0/topics/i18n/translation/#django.conf.urls.i18n.i18n_patterns
urlpatterns = i18n_patterns(
path('api-token-auth/', views.obtain_auth_token),
...
prefix_default_language=False
)
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'),
)
I haven't found a satisfactory way of doing this: I have a djangocms setup that is working fine. But I need to add content from a table outside the CMS to my homepage and render that content on the template. I can do this, but editing the urls.py within CMS to use my views like so...
url(r'^', 'myapp.views.slideshow_info'),
... excludes any content from CMS. I understand that I just get my custom views to accommodate what CMS' views is doing, but how do I achieve this?
at the moment my app's views says:
from myapp.models import model1, model2
def slideshow_info(request):
return render_to_response('index.html', {'slideshow_list' : model1.objects.all()})
Many thanks
You can hook a custom app instance to any Django-CMS page. Here's the documentation on how to do so: http://docs.django-cms.org/en/2.1.3/extending_cms/app_integration.html#app-hooks You shouldn't need to alter the base url patterns to specifically re-route / to your view.
Before custom app-hooks were available, I would accomplish what you're trying to do with template tags.
Hope that helps you out.
Followup
Ok, in a recently completed site, I had to hook an app titled "portfolio" to display images on the home page of a Django-CMS site.
Here are the relevant portions of the code:
#portfolio/cms_app.py
from django.utils.translation import ugettext_lazy as _
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
class PortfolioAppHook(CMSApp):
name = _('Portfolio')
urls = ['portfolio.urls']
apphook_pool.register(PortfolioAppHook)
#portfolio/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('portfolio.views',
url(r'^(?P<slug>[-\w]+)/$', 'project_detail', name='project_detail'),
url(r'^$', 'portfolio_index', name='portfolio_index'),
)
#portfolio/views.py
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render
from portfolio.models import Project
def portfolio_index(request):
project_objects = Project.for_public if request.user.is_anonymous() \
else Project.objects
projects = project_objects.all().select_related(depth=1)
return render('portfolio/index.html',
{'projects' : projects}, request)
def project_detail(request, slug):
project = get_object_or_404(Project, slug=slug)
if not project.public and request.user.is_anonymous():
return HttpResponseRedirect('/?login=true')
return render('portfolio/project_detail.html',
{'project' : project}, request)
#urls.py (base urls)
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
from views import login_user, logout_user
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/filebrowser/', include('filebrowser.urls')),
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^login/$', login_user, name='login_user'),
url(r'^logout/$', logout_user, name='logout_user'),
(r'^', include('sorl.thumbnail.urls')),
(r'^', include('cms.urls')),
)
if settings.SERVE_STATIC_MEDIA:
urlpatterns += patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
As you can see from this working example, I haven't altered my base URLs to accommodate the home page view, rather I've provided the URLs for my Portfolio app to Django-CMS through cms_app.py
Hope that gets you going.