Can't figure out why included urls isn't working. Project has 1 my app. So in myproject/mysite/urls.py i have:
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
......
url(r'^sub/', include('subscription.urls')),
url(r'^', include('cms.urls')),
)
Then in myproject/subscription/urls.py:
from django.conf.urls import patterns, url
from .views import subscribe
urlpatterns = patterns(
url(r'^subscribe/', subscribe),
)
If i try to go to http://localhost:8000/lt/sub/subscribe/ it displays 404 page (Page not found). What might be the problem?
EDITED: Project tree:
myproject/
manage.py
media/
static/
subscription/
templates/
__init__.py
admin.py
forms.py
models.py
urls.py
views.py
mysite/
locale/
static/
templates/
__init__.py
settings.py
urls.py
wsgi.py
You have an error in calling the patterns() function:
urlpatterns = patterns(
url(r'^subscribe/', subscribe),
)
patterns() accepts a view prefix as the first argument, if you pass an url() instance, this will not be used as a url pattern. If you had any other url patterns, this would give you an error, but in this specific case patterns() will simply return an empty list.
Since patterns() is deprecated, it is better to switch to the new-style url configuration, and use a list:
urlpatterns = [
url(r'^subscribe/', subscribe),
]
Otherwise you'd have to pass a prefix as the first argument. Since you don't actually use the prefix (you pass a view function, not the import location as a string), this would generally be the empty string ''.
if i try localhost:8000/sub/subscribe, it appends url with /lt/
Thats because your url pattern requires a trailing slash, which you haven't provided
r'^subscribe/' # Shouldn't have the slash or should be optional
Normally this wouldn't be a problem since djangos APPEND_SLASH would help you out (if you're using CommonMiddleware) but it would seem that django-cms has a catch-all redirect applied on your application to redirect to a locale.
Related
As mentioned here,
the include() function allows referencing other URLconfs. Note that the regular expressions for the include() function don't have a $ (end-of-string match character) but rather a trailing slash. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
The idea behind include() is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (polls/URLs.py), they can be placed under /polls/, or under /fun_polls/, or under /content/polls/, or any other path root, and the app will still work.
Need clarification on second point(above) about, include() is to make it easy to plug-and-play URLs,
For one of the app(webapp) in Django project:
webapp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.py
where, root URLconf is pointing to webapp.urls as shown below,
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^webapp/', include('webapp.urls')),
]
here is the complete code
With webapp app, How include() can be used in creating plug and play URLS?
Maybe in a django project, you will register many apps into it, and each will have its own urlconf, at that moment, using the include() will make things simple.
You could just add a prefix for each app and using include() to combine all into root urlConf.
Whenever Django encounters include(), it chops off whatever part of
the URL matched up to that point and sends the remaining string to the
included URLconf for further processing.
The include() works by import_module(), you could refer to this function at this
In Django URLs, include() lets you refer to other app's URLconfs.
It basically means something like this
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^webapp/', include('webapp.urls')),
]
webapp.urls.py
urlpatterns = [
url(r'^$', views.content),
]
Now, whenever you will call /webapp/ it will render the content view.
Plug-and-Play basically means URL /webapp/ is ready to be called. It will return anything that will come under this URL if you have a view specified for this URL.
I'm developing a webpage with Django.
I want to add/define an url for the application.
At this moment the url (dsvd/) doen't work properly. It shows only table data but no css and background.
here is the code for the main urls.py file.
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('',
# Examples:
# url(r'^$', 'kleedkamer_overview.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('kleedkamer_overview.urls')),
url(r'^dsvd/', 'kleedkamer_overview.views.indeling'),
)
and here the code inside the application urls.py file
from django.conf.urls import patterns, url
from kleedkamer_overview import views
urlpatterns = patterns ('',
url(r'^$',views.indeling, name='indeling'),
)
Is there anyone who can help me find the problem?
Greetz.
It looks like line is giving you trouble:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
It seems like you are trying to map this URL to kleedkamer_overview.views.indeling but the URL regrex does not end with a '$'. Urls.py files that are mapped to other apps look like this:
url(r'^admin/', include(admin.site.urls))
Notice the include function call and that the regrex expression r'^admin/' does not end with a $
However mapping to a specific view is a bit different and it looks like this:
url(r'^$', 'kleedkamer_overview.views.home', name='home')
Notice that this time, where the include() function call would go you are instead telling Django which specific view you want to use and that the site regrex is r'^$' ending with a $.
Try changing this:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
to this:
url(r'^dsvd/$', 'kleedkamer_overview.views.indeling')
Edit:
I saw your comments and while the urls.py is not the source of your problem, what I said is still valid because the urls.py was malformed. You should not even have the offending line there at all because you have already included the urls.py from kleedkamer_overview , there is no need to include it twice. It isn't DRY and it is just bad practice in general. This is why this still works despite being malformed because sites are looked for in order, in this case first it looks for /admin then / and does not reach /dsvd because it was already caught by / and your malformed url mapping is NEVER reached.
I'm new in Django and use this tutorial for learning https://docs.djangoproject.com/en/1.6/intro/tutorial03/
Have a little problem with routing in.
View code:
def index(request):
return HttpResponse(r'<h3 style="font-style: bold;">Index</h3>')
URL config code:
1. blog/urls
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
2.project/urls
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
When i come for 127.0.0.1:8080/index that
Page not found (404)
Using the URLconf defined in les1.urls, Django tried these URL patterns, in this order:
^blog/
^admin/
The current URL, index, didn't match any of these.
Struct of project
blog/
templates
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
les1/
__init__.py
settings.py
urls.py
wsgi.py
db.sqlite3
manage.py
Can't find error:(
What is the actual url you enter in your browser ? is it 127.0.0.1:8000 or 127.0.0.1:8000/index ? From your post, I assume it is the second:
The current URL, index, didn't match any of these.
What if you enter 127.0.0.1:8000/blog ?
index is the name of your route, used only on server-side. It's a shortcut used for quick reversing and displaying of URL:
from django.core.urlresolvers import reverse
print(reverse('index'))
# will output "/blog", which is the actual URL
Remove r from view code.
def index(request):
return HttpResponse('<h3 style="font-style: bold;">Index</h3>')
I plan on having several different apps inside my django project, each for a new technology I want to play around with. As I work on each on, I want to have the root URL path redirect to the project I'm working on.
Directory Structure:
backyard/
my_project/
views.py
backyard/
urls.py
backyard/backyard/urls.py:
from django.conf.urls import patterns, include, url
from django.shortcuts import redirect
urlpatterns = patterns('',
url(r'^$', redirect('my_app/')),
url(r'^my_project/$', 'my_project.views.homepage'),
)
backyard/my_project/views.py
def homepage(request):
return render_to_response('my_project/index.html', {'data':data})
When I access the page at http:machine-name:8000/ I get an error saying The included urlconf backyard.urls doesn't have any patterns in it I most definitely have URLs in my urls.py, what is the issue?
A URLconf needs to map regexes to views -- the redirect function doesn't return a view; rather, it returns an HttpResponse. You can look at this question for an example of how to define a redirect directly in the URLconf.
please tell me how to move the view (view.py) in the directory "views".
Now the structure of my folders and files is as follows:
proj1(catalog)
views.py(file)
manage.py(file)
proj1(catalog)
wsgi.py(file)
urls.py(file)
settings.py(file)
__init__.py(file)
views(catalog)
urls.py following content:
from django.conf.urls import patterns, include, url
import views
urlpatterns = patterns('',
url('^$', views.hello),
url('^datetime$', views.current_datetime),
url('^dt$', views.current_datetime),
url('^dt/(\d{0,2})$', views.current_datetime2),
)
I need to file located in the directory view.py proj1/proj1 /.
wherein static pages is still made available to the browser.
the problem is that I do not know how to change the code in the files: urls.py, settings.py
There is no need to change views, just do in settings.py
TEMPLATE_DIRS = (
'/path/to/proj1/proj1/views/', # or another path with templates
'django/contrib/admin/templates/'
)