i have installed DJANGO 1.5, once entered 127.0.01:8000 the beautiful HTLM page appears.
"it worked"
now whatever i enter in the Browser URL, it always goto default welcome page.
once i start playing with url.py this functionality get vanished. and i start getting 404 page.
is there any way to keep this functionality on i.e what ever is typed in the browser url it goes to main page exception for the defined url in url.py
please help
url.py
from django.conf.urls import patterns, include, url
from article.views import HelloTemplate
urlpatterns = patterns('',
url(r'^hello_template/$', 'article.views.hello_template'),
url(r'^hello_template_simple/$', 'article.views.hello_template_simple'),
other code snippet for "myproject/urls.py"
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
urlpatterns = patterns('',
(r'^myapp/', include('myproject.myapp.urls')),
(r'^$', RedirectView.as_view(url='/myapp/list/')),
(r'', 'myproject.myapp.views'),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Just put a your default page as a catch-all URL at the end of the other URLs:
urlpatterns = patterns('',
url(r'^hello_template_simple/$', 'article.views.hello_template_simple'),
url(r'', 'article.views.hello_template'),
Now any URL which isn't matched by hello_template_simple will be caught by hello_template.
Related
I've run into a problem configuring the url.py files in a new project. I have one app, which contains two views. The first view should appear at myurl.com, while the other should appear at myurl.com/foo. myurl.com appears without trouble but myurl.com/foo shows a 404 page not found error.
The url.py at the project level looks like this:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('myapp.urls', namespace="myapp")),
)
And the url.py at the app level looks like this:
from django.conf.urls import patterns, include, url
from myapp import views
urlpatterns = patterns('',
url(r'^$', views.book_search, name='book_search'),
url(r'^foo/', views.myapp, name='myapp')
)
I understand that that django is taking the URL that is submitted and checks it against the url patterns defined at the project level, but I don't know how to direct it to myapp without hosting all of myapp at some url that is not at the root, i.e. myapp.com/bar and myapp.com/bar/foo.
url(r'^$', include('myapp.urls', namespace="myapp")),
Remove the ^$ here. This would force all included URLs to match only if starting with "the end", i.e. nothing.
I am trying to display contents of a static page in Django project.
urls.py :-
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'spollow.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^$', 'django.views.generic.simple.direct_to_template', {'template': 'index.html'}),
url(r'^admin/', include(admin.site.urls)),
)
index.html is in the same directory as urls.py
I am getting 500 internal server error. Any ideas where I am going wrong?
First of all, what is the stacktrace from the 500 error saying that the error may be? You may be using Django 1.6 and the call to direct_to_template is deprecated.
On Django 1.5 or newer you can use TemplateView
Here's the example from the documentation
https://docs.djangoproject.com/en/dev/topics/class-based-views/
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^about/', TemplateView.as_view(template_name="about.html")),
)
You can use the direct_to_template view on Django 1.4 or older
Here's the relevant documentation
https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-simple-direct-to-template
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^foo/$', direct_to_template, {'template': 'foo_index.html'}),
(r'^foo/(?P<id>\d+)/$', direct_to_template, {'template': 'foo_detail.html'}),
)
If it is the latter, I would use a module instead of string, (look at the import on the example).
Other than that, without the 500 details it will be shooting in the dark, you may not have the right template, or an incorrect path, or a million different things.
Bonus note
If you just want to serve static pages, it might be better to serve them through the actual webserver in front of django (nginx, apache, etc), specially if you are expecting a high volume of traffic.
If Your error is due to unable to find index.html
if yours is an app(ie: created by python manage.py startapp <app>) then:
Then django will search for template files in <app>/templates directory, if you added the app to INSTALLED_APPS in settings.py.
so you need to create a folder templates inside your <app> and put index.html inside it.
if you don't have any apps, you want to add the template path manually :
open settings.py, then edit TEMPLATE_DIRS
TEMPLATE_DIRS = (
# Put the full path of the template dir here, like "/home/html/django_templates" or
# "C:/www/django/templates".
)
In Django 1.5 or newer you can use the render function instead of direct_to_template:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'django.shortcuts.render', {'template_name': 'index.html'}),
)
Or if you prefer the more complex way :), you can use class-based TemplateView:
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="index.html")),
)
I have a URL that does not work, for some reason. I get a 404, "'new' could not be found". Here is my urls.py:
url(r'^assets/new', 'watershed.views.new_asset', name='new_asset'),
There is a lot more in my urls.py but this is the ONLY one that contains the word, "assets" in it. If I change this url to anything/new, it works. If i misspell assets (assettss/new), it works. If I take out the /new and just use "assets", it also works fine. In my views folder I have an __ init __.py which contains the following:
from groups import *
from members import *
from leave_group import *
from payments import *
from assets import *
I also have an assets.py, which contains the following:
from django.contrib.auth.decorators import login_required
from watershed.models import Member, Org, OrgToMember, Asset
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
def new_asset(request):
return render(request, 'asset_add.html')
I have no idea what Django does not like about assets/new.
UPDATE: Here is my full urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^', include('outside.urls')),
url(r'^blog', include('blog.urls')),
url(r'^admin', include(admin.site.urls)),
url(r'^logout', 'watershed.views.logout', name='logout'),
url(r'^register/create', 'watershed.views.create', name='create'),
url(r'^register', 'watershed.views.register', name='register'),
url(r'^translog/(\d+)', 'watershed.views.translog', name='translog'),
url(r'^settings', 'watershed.views.settings', name='settings'),
# Group URIs
url(r'^groups/(\d+)/leave', 'watershed.views.leave_group', name='leave_group'),
url(r'^groups/(\d+)/dissolve', 'watershed.views.dissolve_group', name='dissolve_group'),
url(r'^groups/new', 'watershed.views.add_group_form', name='add_group_form'),
url(r'^groups/(\d+)', 'watershed.views.dashboard', name='dashboard'),
url(r'^groups/add', 'watershed.views.add_group', name='add_group'),
url(r'^groups', 'watershed.views.groups', name='groups'),
# Member URIs
url(r'^members/(\d+)', 'watershed.views.profile', name='profile'),
url(r'^member/login', 'watershed.views.login', name='login'),
# Payments URIs
url(r'^payments', 'watershed.views.payments', name='payments'),
# Asset URIs
url(r'^assets/new', 'watershed.views.new_asset', name='new_asset'),
You new_assets function containts in assets.py file, buy you import this function from views.py file. Use this:
url(r'^assets/new', 'path.to.assets.new_asset', name='new_asset'),
I figured it out. The problem is that my static url in my settings.py is - wait for it:
STATIC_URL = '/assets/'
So, clearly, one of those must change.
I've been searching around but can't figure out why the redirection after login is not working as expected.
I successfully login at http://localhost:8000/accounts/login/. After that I'm always redirected to http://localhost:8000 no matter if I use settings.py variable LOGIN_REDIRECT_URL or if I leave it completely out.
# urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^grappelli/', include('grappelli.urls')),
url(r'^$', 'myapp.views.home', name='home'),
url(r'^home/$', 'myapp.views.apphome', name='apphome'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('registration.backends.default.urls')),
(r'^admin/', include("massadmin.urls")),
)
I don't know where I should start looking for a problem. Any ideas? Is there some settings I'm missing here? Any other files I should include to help debug the problem?
Check your form action! (glad I could help :) )
I have a project with three applications installed. The first (photologue) is working fine, but I'm having problems with the last two. My urls.py file in the Django site looks like this:
from django.conf.urls.defaults import patterns, include, url
from django.contrib import auth
from django.contrib import admin
from funvisis.users.models import FVISUser
admin.site.register(FVISUser)
admin.autodiscover()
urlpatterns = patterns(
'',
(r'^admin/', include(admin.site.urls)),
(r'^photologue/', include('photologue.urls')),
(r'^inspeccionespuentes/', include('funvisis.bridgeinspections.urls')),
(r'^inspeccionesedificios/', include('funvisis.buildinginspections.urls')),
)
The urls.py file on both of my applications looks like:
from django.conf.urls.defaults import patterns, include, url
from django.conf import settings
from .admin import admin_site
from .views import csv_view
urlpatterns = patterns('',
url(r'^csv/(?P<models_url>\w+)/', csv_view),
(r'', include(admin_site.urls),
)
The problem arise when I try to reach the url "^inspeccionesedificios/", since there is no link to add a new buildinginspection and the link to list all the inspections is formed as "http://127.0.0.1:8000/inspeccionespuentes/buildinginspections/" (note how it starts with "inspeccionespuentes" rather than "inspeccionesedificios").
If I change the order of the patterns in the Django site from:
(r'^inspeccionespuentes/', include('funvisis.bridgeinspections.urls')),
(r'^inspeccionesedificios/', include('funvisis.buildinginspections.urls')),
to:
(r'^inspeccionesedificios/', include('funvisis.buildinginspections.urls')),
(r'^inspeccionespuentes/', include('funvisis.bridgeinspections.urls')),
results in the same behaviour but with the problem in "inspeccionespuentes".
I have recently migrated from Django 1.3 to Django 1.4 and this problem ain't appeared before the migration. Any idea?
Thanks!