i created my first app in Django. and when i am running it in my chrome browser using the url
http://127.0.0.1:8000/hello
It didn't open but when i used the url:-
http://127.0.0.1:8000/
It worked, don't know why. I added the url in urls.py file. like this.
path('hello', views.hello_world_view, name='hello')
suggest me where i am going wrong.
Thanks
URL patterns require a trailing slash in order to work
path('hello/', views.hello_world_view, name='hello')
Write like this
path('hello/', views.hello_world_view, name='hello')
read this djangoproject for URLs dispatcher it will help you how to configure URL into Django
Related
I want to add a field for users to edit their profile pictures and checked out a few tutorials. However, every tutorial I've found included some form of this in urls.py:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
Why can't I do this with deployment and if it has to be like this? Is there any way for me to allow users to upload and modify their profile pictures?
Any help would be appreciated. Thanks.
This is because in production mode django stop serving static files.
Thus static url will result in a page not found.
Hello everyone I want to redirect URLs like this
from
https://www.example.co.uk/pages/terms-conditions
to
https://www.example.co.uk/terms-conditions
I developed the updated site using the Django framework and now want to redirect old site URLs to new URLs and not losing traffic. what is the best way of doing…?
New site is not published it is on local machine
my view.py
my urls.py
I guess this is not best practice but you can just redirect the user to the right url if he hits the other one in your views.py like:
def old_view(request):
return redirect('https://www.example.co.uk/terms-conditions')
def new_view(request):
return render(request, "your_app/conditions.html")
with urls.py like:
path('pages/terms-conditions/', views.old_view, name='old_view')
path('terms-conditions/', views.new_view, name='new_view')
Assuming you want to remove the application name from the urls. In your project's urls.py NOT your applications urls.py. You should have a line similar to the following:
path('pages/', include('pages.urls', namespace='pages')),
replace the 'pages/' with just ''.
Note: Using the pages/ allows you to provide segregation between your applications to ensure the urls do not conflict
I'm currently building out an authentication server using Django, djangorestframework, and django-rest-auth. My problem is actually pretty simple I think, but I haven't really been able to find any resources on it.
Here's my issue, in django-rest-auth there is a specific url to change a user's password, which is /rest-auth/password/reset/. I'd like the url to instead be /auth/password/change/, but don't want to edit the library code to do so.
The issue is that as of now, in my url.py file, I have the rest-auth urls imported as such:
from django.urls import path, include
urlpatterns = [
path('', include('rest_auth.urls')),
]
So it just imports the urls as written in the library. How can I change the specific url to what I want?
You just need to add a url like this
path('/auth/password/change/', your_view)
your_view will be same as view of /rest-auth/password/reset/ which is PasswordResetView.
I'm trying to run this library. I downloaded and copied it on my desktop, then i tried to run it using:
py -3.7 manage.py runserver
I got:
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
But when i go to that url i keep getting this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in django_google_authenticator.urls, Django tried these URL patterns, in this order:
^admin/login/$
^admin/
The empty path didn't match any of these.
I don't understand what's wrong, can someone help me? Thanks in advance!
I think thats the normal behavour of Django, as you can see it is saying that your path didn't match any of the configured ones, like admin, so why don't you try http://127.0.0.1:8000/admin/to access Django Admin login page.
If you want to see something different from a 404 with the base url you need to configure your urls and maybe add a redirect view, in that way when the base url is use it will redirect to your admin or other preferred page.
Here is my current method of serving robots.txt
url(r'^robots\.txt/$', TemplateView.as_view(template_name='robots.txt',
content_type='text/plain')),
I don't think that this is the best way. I think it would be better if it were just a pure
static resource and served statically. But the way my django app is structured is that the static root and all subsequent static files are located in
http://my.domain.com/static/stuff-here
Any thoughts? I'm amateur at django but
TemplateView.as_view(template_name='robots.txt',
content_type='text/plain')
looks a lot more resource consuming than just a static call to my static directory which is served on nginx.
Yes, robots.txt should not be served by Django if the file is static. Try something like this in your Nginx config file:
location /robots.txt {
alias /path/to/static/robots.txt;
}
See here for more info: https://nginx.org/en/docs/http/ngx_http_core_module.html#alias
Same thing applies to the favicon.ico file if you have one.
The equivalent code for Apache config is:
Alias /robots.txt /path/to/static/robots.txt
I know this is a late reply, I was looking for similar solution when don't have access to the web server config. So for anyone else looking for a similar solution, I found this page: http://www.techstricks.com/adding-robots-txt-to-your-django-project/
which suggests adding this to your project url.py:
from django.conf.urls import url
from django.http import HttpResponse
urlpatterns = [
#.... your project urls
url(r'^robots.txt', lambda x: HttpResponse("User-Agent: *\nDisallow:", content_type="text/plain"), name="robots_file"),
]
which I think should be slightly more efficient that using a template file, although it could make your url rules untidy if need multiple 'Disallow:' options.