Django urls.py not updating to include heroku url? - django

I am trying to deploy my Django app on Heroku and I am following the tutorial but when I go and try to open the website with:
heroku open
I receive the error :
Request URL: http://nameless-dawn-7713.herokuapp.com/
Using the URLconf defined in crunchWeb.urls, Django tried these URL patterns, in this order:
^crunchApp/results$
^crunchApp/contact$
^admin/
The current URL, , didn't match any of these.
Even though before running " heroku open" my URLs.py file looks like this (saved) :
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('crunchApp.views',
url(r'^crunchApp/results$', 'results'),
url(r'^http://nameless-dawn-7713.herokuapp.com/$', 'contact'),
url(r'^crunchApp/contact$', 'contact'),
url(r'^admin/', include(admin.site.urls)),
)
I think I might have to change the urls.py before the command:
git commit -m "my django app"
but I dont know the URL that it will need until the command:
heroku create
Any ideas of what to do?

Why do you have url(r'^http://nameless-dawn-7713.herokuapp.com/$', 'contact') in your urls file ? In fact, these URLs should be relative to your application, and you should not update this file before uploading to Heroku (if it works locally, it should work on Heroku without any change to this file).
If you want to connect the contact view to your base URL (http://nameless-dawn-7713.herokuapp.com/), you should use something like:
url(r'^$', 'contact')
Hope this helps !

Related

First view in DJANGO tutorial -> Page not found (404)

I was doing tutorial "Writing your first Django app, part 1" and I got stuck in "Write your first view". I have fulfil all instructions regardings files. When I go to http://127.0.0.1:8000/ then I can see "The install worked successfully! Congratulations!" but if I go to http://localhost:8000/polls/ then I see:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
In this case according instructions I should see "Hello, world. You're at the polls index". I attached 2 files content and 2 interesting screens (why does it emphasize?).
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
I was looking answer in Stack Overflow (also in hints) or YouTube but unfortunately without success. Interesting but in another computer it works, but on my laptop not.
So you are doing the URLS incorrectly, or partially at least.
You need to create another app in django called polls:
python manage.py startapp polls
Then in the polls/urls.py file, include an extension for /polls, that references your view in polls/view.py:
urlpatterns = [
path('/polls', views.<view-name>, name='Polls URL')
]
Then it should work, otherwise you are doing something wrong and need to elaborate on our problem.
Note that Django's DEBUG should be True when developing at all times, so you can see the full error log. DEBUG=FALSE is used if you have a webapp public (aka deployed/in production.
Subject closed. Solution -> Bad configuration urls.py file in mysite and polls. Check content both files in tutorial context.

Django Page Not found error : Sudden in process of learning

I was in motion of learning Django, suddenly encountered Page not found Error in all pages.
This is my URL pattern file code:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^analysis/', include('analysis.urls')),
url(r'^music/', include('music.urls')),
]
but while accessing any of the pages page not found error is coming:
I have run the server on port 8084,
python .\manage.py runserver 8084
Accessing urls:
http://127.0.0.1:8084/admin
http://127.0.0.1:8084/music
http://127.0.0.1:8084/analysis
All of the views are available and pages were previously displayed but now it is not working. I have tried re-starting the server also but no help.
Kindly suggest me what could be the possibility of error.
Try running the runserver like this:
python manage.py runserver 8084

Runserver: " Not Found: / "

I executed runserver to test Django Framework but it appears a message " Not Found: / ". When I try the page localhost:8000 it works fine, but the message is still there. Any idea?
P.S. I tried localhost:8000/admin and the message does not appear.
Urls.py
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
This happens simply because you didn't define any pattern in urls.py that matches / and you have some views that matches admin/.
If you want to show something in http://127.0.0.1:8000/ you will need to create a view first and add it to urls.py like
`url(r'^$', 'myview', name='myview'),``
I recommend you to follow the Django tutorial

My DJango app is responding to /static urls

Update
I figured out what was causing the stylesheets to become invisible, though I don't quite understand it all. I set DEBUG=False in settings.py to test the error handling as described in the tutorial. Somehow setting debug to false causes the static files not to be locatable. I will look further in the configs to see if I can get a clear understanding why. Until then, please feel free to answer or comment with additional info. I am still learning!
Update
I'm going through a DJango tutorial from here and I hit a roadblock. I'm up to tutorial 3 where they explain how to refactor your urls.py file when I try loading up the admin site to make sure I haven't broken it. Sure enough it looked all wierd because it was missing the stylesheets. Stylesheets are pulled from here:
http://127.0.0.1:8000/static/admin/css/base.css
When I hit that link in my browser I get the custom 404 page I configured for my app. The stylesheets were working prior but I'm not sure which change broken them. I went through my urls.py file and reverted all of the polls specific url configs to no avail. Here's my current urls.py under hellodjango (the name of my project.)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.http import HttpResponse
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
def page_not_found(request, template_name='404.html'):
return HttpResponse("Could not find the resource you asked for...")
handler404 = 'hellodjango.urls.page_not_found'
and here's the urls.py under my polls directory:
from django.conf.urls import patterns, url
# Uncomment the next two lines to enable the admin:
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Help?
It looks like you don't have a URL pattern for /static. As such, the static/admin/css/base.css URL doesn't match any pattern, and so you get a 404. Try something like this:
from django.conf.urls.static import static
# ...
urlpatterns = patterns('',
# ...
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
# ...
This should work for you -- go to /static/foo.css, and you should see your CSS.
It's worth noting that this is discouraged in a production environment. For your tutorial app, though, it'll work.
The staticfiles app provides a custom runserver management command that automatically serves the static files, are you sure you have the following in your settings?
INSTALLED_APPS = (
# ...
'django.contrib.staticfiles',
)
In production, you'll use the collectstatic management command that finds all of the static media and dumps it into STATIC_ROOT (this is the only purpose for this setting - it isn't used or needed during development).
Glad you figured it out. Here's why it works like this.
django.contrib.staticfiles overrides the runserver management command so that the static files are served automatically. To remind people that they shouldn't be using django to serve static files, this only happens when DEBUG = True, as you found out.
The documentation of the overridden management command explains that you can use the --insecure flag to make this work no matter the state of the DEBUG setting.

Redirect in Django admin page

In my django project, after activating the admin application, I canno't get access to the page localhost/soundaxis/admin because I'm redirected to localhost/admin.
I think the problem has to do with the fact that the project was not located at the base address.
Urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^soundaxis/', 'views.index.home'),
(r'^soundaxis/admin/', include(admin.site.urls)),
)
Start the server by typing the following in the base dir:
python manage.py runserver
then go to http://localhost:8000/
Its rather complicated to get the project to run off another subdirectory so I suggest running it from the base, especially during development. Later in the game you can try work it from a subdir if required, but better yet would be subdomain.