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
Related
Okay... let's try to explain things clearly. I've used Python Django to create a dynamic webpage/web-app. After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. When I access my website, ordinanceservices.com, i get an error 404; however, if I type ordinanceservices.com/home it works as it should and displays the home page. How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary.
Here is the specific error:
Page not found (404)
Request Method: GET
Request URL: http://ordinanceservices.com/
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
^admin/
^ ^home/ [name='home']
^ ^contact/ [name='contact']
^ ^services/ [name='services']
The current URL, , didn't match any of these.
I somewhat understand what is happening here though I do not know how to fix this. Next I will provide my .urls files for each folder that contains such:
/django_project urls.py
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('company.urls')),
)
/company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
/company views.py
from django.shortcuts import render
def home(request):
return render(request, 'company/home.html')
def contact(request):
return render(request, 'company/contact.html')
def services(request):
return render(request, 'company/services.html')
What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; preferably the home page of my webpage.
I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. I am not certain but I have been trying to figure this out for hours. Does anyone have a clue what I can do to fix this?
You haven't defined anything at the root url. Add one more line to your company urls.py so it becomes
//company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py
and then in your company/urls.py do this
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^services/$', views.services, name='services'),
]
and in your main urls.py add this code
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
]
I'm trying out Python Django. With eclipse pyDev. But I'm unable to simply get my first url to display.
This urls.py is from the Cr package.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
This urlspy is from the Crowd package.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'Crowd/', include('Cr.urls'))
]
So what I've understood from this the Crowd package is the "main" webservice(?), and by using include I can whenever the regular expression matches Crowd, will pass it on to the other urls.py(Cr). But the debugger passes:
Using the URLconf defined in Crowd.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, crowd, didn't match any of these.
my views.py file
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse('<h1>Hello World!</h1>')
I tried to access it with http://127.0.0.1:8000/Crowd
Below is an image of the project folder.
Can we see your settings.py file? There is a place in there where you define your project's url file. I'm assuming it's right now either not there or it's pointing to the wrong place, because Django can't find your urls.py file.
For example, in my settings.py file for one of my projects, I have:
ROOT_URLCONF = 'Freya.urls'
Where "Freya" is my project name
Just for reference, not that I know this will solve your problem, this is what (part of) my urls.py file looks like for one of my projects:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'Crowd/', include('Cr.urls'))
]
just use this urls.py file
I'm new to Django, and my first major project of sorts is to go through an existing Django Web App to update it, streamline it and optimize it in any way that I can. I'm having some difficulty in getting it to run on my machine, however.
When navigating to the local development server at localhost:8000/ I get the following error:
Using the URLconf defined in core.urls, Django tried these URL patterns, in this order:
^home/$ [name='portal']
^agentexoplanet/admin/
^agentexoplanet/agentex/ [name='agentex_redirect']
^agentexoplanet/admin/agentex/event/(?P<planetid>\d+)/calibrators/(?P<calid>\d+)/$ [name='agentex_admin_calib']
^agentexoplanet/admin/agentex/event/(?P<planetid>\d+)/calibrators/$ [name='agentex_all_calib']
^agentexoplanet/admin/
^agentexoplanet/
^static/(?P<path>.*)$
The current URL, , didn't match any of these.
This is the core.urls file:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles import views
from django.views.generic import RedirectView
from django.contrib.auth.views import login, logout
from agentex import urls
from agentex.views import home
from agentex.admin import calibrator_check, allcalibrators_check
#from admin.site import urls
#from showmestars.views import newimage, latestimages
admin.autodiscover()
urlpatterns = [
#(r'^api/', include('odin.api.urls')),
url(r'^home/$', home, name='portal'),
url(r'^agentexoplanet/admin/', include(admin.site.urls), name='agentexo_admin'),
url(r'^agentexoplanet/agentex/', RedirectView.as_view(url='/agentexoplanet/'), name='agentex_redirect'),
url(r'^agentexoplanet/admin/agentex/event/(?P<planetid>\d+)/calibrators/(?P<calid>\d+)/$',calibrator_check, name='agentex_admin_calib'),
url(r'^agentexoplanet/admin/agentex/event/(?P<planetid>\d+)/calibrators/$',allcalibrators_check, name='agentex_all_calib'),
url(r'^agentexoplanet/admin/', include(admin.site.urls), name=''), # QUERY
url(r'^agentexoplanet/',include(admin.site.urls), name='agentexo_urls'),
#url(r'^showmestars/newimage/$', newimage, {'eventid':0}, name='showmestars_newimage'),
#url(r'^showmestars/(?P<eventid>\w+)/$', latestimages, name='showmestars_latestimage'),
#url(r'^showmestars/$', latestimages, {'eventid':0}, name='showmestars_latestimage_event'),
#url(r'^login/$',login, name='site_login'),
#url(r'^logout/$',logout, name='site_logout'),
]
if settings.DEBUG:
urlpatterns += [
url(r'^static/(?P<path>.*)$', views.serve),
]
And my project (agentex) urls file:
from django.conf.urls import include, url
from django.contrib.auth.views import login, logout
from .views import *
from django.conf import settings
urlpatterns = [
url(r'^$',index, name='index'),
url(r'^account/login/$', login, {'template_name' :'login.html'}, name='login'),
url(r'^account/logout/$', logout,{'template_name' :'logout.html'}, name='logout'),
url(r'^account/register/$', register, name='register'),
url(r'^account/$', editaccount, name='editaccount'),
url(r'^profile/$',profile, name='profile'),
url(r'^planets/$',target, name='target'),
url(r'^fitsanalyse',fitsanalyse, name='fitsanalyse'),
url(r'^test',tester, name='tester'),
url(r'^briefing/read/$',read_manual_check, name='read_manual_check'),
url(r'^briefing/$',briefing, name='briefing'),
url(r'^comment/$',addcomment, name='addcomment'),
url(r'^(?P<code>\w+)/view/$',addvalue, name='addvalue'),
url(r'^(?P<code>\w+)/graph/update/$',updatedataset, name='updatedataset'),
url(r'^(?P<code>\w+)/lightcurve/advanced/$',graphview, {'mode' : 'advanced','calid':None}, name='advanced-graph'),
url(r'^(?P<code>\w+)/lightcurve/me/$',graphview, {'mode' : 'simple','calid':None}, name='my-graph'),
url(r'^(?P<code>\w+)/lightcurve/calibrator/update/$',classifyupdate, name='classifyupdate'),
url(r'^(?P<code>\w+)/lightcurve/calibrator/$',graphview, {'mode' : 'ave','calid':None}, name='average-graph'),
url(r'^(?P<code>\w+)/lightcurve/calibrator/(?P<calid>\w+)/$',graphview, {'mode' : 'ave'}, name='calibrator-graph'),
url(r'^(?P<code>\w+)/lightcurve/$',graphsuper,name='super-graph'),
url(r'^(?P<code>\w+)/$',infoview, name='infoview'),
url(r'^(?P<code>\w+)/data.(?P<format>\w+)',measurementsummary, name='measurementsummary'),
]
I'm not entirely sure of the error here. My core.urls file links to index.html, but I imagine it isn't linking correctly. Does anybody have any ideas?
Thanks in advance (and apologies for not posting an image - StackOverflow wouldn't let me).
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).
I'm new to Django/Python and I'd like to redirect some URLS to another domain.
mydomain.com/blog needs to redirect to 'http://blog.mydomain.com'
How can I go about doing this?
from django.conf.urls import patterns, include, url
from django.views.generic.base import TemplateView
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name='home'),
url(r'^blog/$', TemplateView.as_view(template_name='pages/blog.html')),
)
use the RedirectView generic view
url(r'^blog/$', RedirectView.as_view(url='http://blog.mydomain.com')),