I'm new to Django/Python. I'm currently taking a beginners course and I'm having difficulty setting up my URLs and connecting to a local server. I'm using Python version: 3.7.0 and Django v.2
The command line does not give any errors (0), it tells me to go here:
Starting development server at http://127.0.0.1:8000/
Which I believe is the local host, however the site says
This site can’t be reached
127.0.0.1 refused to connect.
Can anyone let me know what I'm missing I would greatly appreciate it so that I can continue on with my studying. I'm using tutorials on YT Django Tutorial and the guy is using an older Django so I think that may be why I'm having trouble. He says we should still be okay to follow through even on the new version of Django.
urls.py
from django.contrib import admin
from django.urls import path
from.import views
urlpatterns = [
path('', views.homepage),
path('admin/', admin.site.urls),
path('about/', views.about),
]
views.py
from django.http import HttpResponse
def homepage(request):
return HttpResponse('homepage')
def about(request):
return HttpResponse('about')
Got it to work. This is what was needed:
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Took me forever to figure this out. :)
There is a little modification is required in your code:
Instead of path('about/', views.about),
write this:
path('about/', views.about,name='about'),
after modify this line , run the server and open this http://127.0.0.1:8000/ URL and add /about and then press enter.
http://127.0.0.1:8000/about
Hope it will work in your case
Related
I just started learning django and running the tutorial part 3, decided to see if I understood the mapping from urls.py to views.py.
I got views and urls to work in the polls app, but then I wanted to make views in the project folder, so I made a views.py file in the project folder (see code below), with a view/function , which I named 'home'.
I then edited the urls.py in the project-folder(see below). run the server, It worked! visiting the url: http://localhost:8000/
it responded:
Hello, world. You're at the HOME PAGE.
BUT.. when I tried to make another view in same views.py called: morn, and adding the url for it, then error (see below),
http://localhost:8000/morn
returning:
localhost refused to connect.
I DID IT EXACTLY THE SAME WAY, so just when I thought I understood it, I didnt get it at all?!?!
The difference between the two views are just their name and path, why doesnt it work then?
on a linux manjaro
Python 3.8.1
Django 3.0.3
#
#this is my urls.py (which I made myself), in the project folder
from django.urls import include, path
from django.contrib import admin
from . import views
#from views import morn
admin.autodiscover()
urlpatterns = [
#lager en index-side
path('', views.home, name='home'),
path('morn/', views.morn, name='morn'),
path('admin/', admin.site.urls, name='admin'),
path('polls/', include('polls.urls')),
]
#
#this is the views.py in myproject folder, same folder as
from django.http import HttpResponse
def home(request):
output = 'Hello, world. You\'re at the HOME PAGE.'
return HttpResponse(output)
def morn(request):
output = 'Hello, world. Youre at the morn-path.'
return HttpResponse(output)
ERROR from konsole running the morn-view:
File "/home/nr1/dev/django/myproject/myproject/urls.py", line 29, in <module>
path('morn/', views.morn, name='morn'),
AttributeError: module 'myproject.views' has no attribute 'morn'
Oh no... I just got it! :)
I have put the views.py file in both of the myproject-folders and the urls.py is just in the inner myproject-folder. I was then updating just the outer views.py-file, when I thought I was in the inner views.py, so, ofcourse, the urls.py couldnt find the new function, it was searching the wrong views.py (outer one).
a lot of frustration for nothing.. but I learnt, I learnt!
I'm new with Django Framework, I've been following few tutorials and trying to create a website using Django 3.0.1 and Pythong 3.8
when I crate my super user using:
python manage.py createsuperuser
I'm able to setup it up, but when I try to access: 'http://127.0.0.1:8000/admin/'
Server can't be accessed(image)
This only happens when attempting to access after entering my username and passwords, as soon as I enter my credentials, the server stops.
This is what my urls.py under the project looks like:
from django.contrib import admin from django.urls import path, include
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('tokens/', include('tokens.urls')) ]
Does someone have an Idea of what the problem is?
Thank you in Advance.
Can't seem to find anything on this so far, I'm guessing I have something misconfigured somewhere.
I have a pre-existing Django app and am trying to use Wagtail to add a blog in. I have installed as per the instructions and I can access the default landing page so it seems to be installed however I'm stuck with the below error when attempting to access the admin for it and am not sure how to proceed. Guessing its something to do with not defining namespaces somewhere, the Wagtail docs say that its compatible with Django v1.11 but their integration documentation is for Django v2 specifically and is using re_path etc.
'wagtailadmin_api_v1' is not a registered namespace
My main project urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls', namespace='blog')),
url(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')),
url(r'^autocomplete/', include(ac_urls, namespace='ac')),
url(r'^', include('website.urls', namespace='website')),
]
My blog app's urls.py (which is fresh from a "python manage.py startapp blog")
from django.conf.urls import url, include
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.core import urls as wagtail_urls
app_name = 'blog'
urlpatterns = [
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
]
When I attempt to access localhost:8000/blog/ I get the default wagtail landing page, if I go to localhost:8000/blog/cms/ I get the above error though. If I go to localhost:8000/blog/documents/ I get a 404 as well.
Not sure where I'm going wrong, have tried using a version of wagtail that still used the Django v1.11 way of routing in urls.py but I got the same error!
I have a Django backend that returns json data. I'm able to get data back on my localhost but got a 404 on production server. I'm running nginx in front of gunicorn server. Any ideas why I'm getting a 404? Shouldn't this be able to work to retrieve json data, or do I need to use django rest framework and implement viewsets to make this work?
Not Found
The requested URL /about was not found on this server.
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^about', about.get_info),
]
about.py
from django.http import JsonResponse
def get_info(req):
return JsonResponse({"test": "hello"})
The problem is inside url.py. The way the rules are defined currently, it would only allow you to open about/ and admin/, i.e. with the / at the end. To fix this, you can define the URLs as following:
urlpatterns = [
url(r'^admin/$', admin.site.urls),
url(r'^about/$', about.get_info),
]
Now you should be able to use both admin/ and admin to access the page.
Over the weekend I attended a hackathon, at which I created a Django app that ran on 127.0.0.1:8000/myapp/list on my local machine with
python manage.py runserver
Today, I tried to launch another Django app that I had made in the past for bug fixes and things, but when I ran manage.py runserver it keeps redirecting me to 127.0.0.1:8000/myapp/list, even though it's a totally separate app.
Anyone know how to resolve this issue?
urls.py for Django app that I'm trying to run:
from django.conf.urls import url
from TweeTest import views
urlpatterns = [
url(r'^$', views.view_home, name='home'),
url(r'^about/$', views.AboutPageView.as_view()),
url(r'^contact/$', views.ContactPageView.as_view()),
url(r'^result/$', views.view_tweet, name = 'result'),
]
urls.py for Django app I made over the weekend:
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myproject.myapp.urls')),
url(r'^$', RedirectView.as_view(url='/myapp/list/', permanent=True)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Is it something to do with the RedirectView line in the second urls.py?
I'm not sure why a totally different Django app is trying to connect to the same URL as the other one... seems strange.
If someone knows the answer to this, that would be greatly appreciated!
Thanks!
UPDATE:
When I try to connect to 127.0.0.1:8000 on incognito mode, it works fine. Could this mean it's a browser issue?
Davis
permanent=True sends a HTTP 301 Permanent redirect to the browser. Browsers tend to cache 301 responses very aggressively, meaning your web browser currently remembers that http://127.0.0.1:8000/ is a permanent redirect to http://127.0.0.1:8000/myapp/list/. Since your browser doesn't know that different Django apps might be running on 127.0.0.1:8000 at different times, it always follows the cached redirect.
You shouldn't use permanent=True unless you know what you are doing. While it can boost the performance on a high-traffic site, it is rarely necessary for low- to medium-traffic sites, and the permanent caching can be a real pain for developers.
Either clear your browser cache, run your Django test server on a different port, or use a different browser.
Have you tried using a different browser or clearing your browser history? This sounds like a browser issue to me.