I'm working the very first example of the Django REST framework (version 2). I've followed the Installation and Example instructions to the letter but when I open the API in my browser at http://127.0.0.1:8000/ per their instructions, I get the error shown below. What am I doing wrong? This example seems pretty elementary.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in conf.urls, Django tried these URL patterns, in this order:
^admin/
^api-auth/
The current URL, , didn't match any of these.
If you're using Django 2.x for following that tutorial, I'm afraid it's not updated. I just did exactly what the tutorial says, and in the first try it fails.
Then, I changed my urls.py file as follow:
before:
path('admin/', admin.site.urls),
path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path(r'^', include(router.urls)),
After:
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('', include(router.urls)),
and then did python manage.py runserver, and it's working properly.
you don't have a main url defined, must define a index url something like this:
urlpatterns = [
path('index', views.index, name='index'),
# the next path cath the empty path url www.example.com
re_path('/?$', views.index)
]
the url http://127.0.0.1:8000/ don't exist in your urlpatterns, that's what django is telling you.
to work in django rest framework you must follow the next flowjob:
create yours models
create a serializer for yours models
create a ApiView, ViewSet or Django view where you use your serializers to respond to user requests.
register your views in you urlpattern
if you want to enter in the rest-framework login URLs use the pathlocalhost:8000/api-auth
Related
I'm trying to deploy a Django App with railways for the first time and I was wondering where exactly will be my landing page. So basically my urls.py looks something like this:
path('home/', views.home, name="home"),
path('blogposts/', views.blogposts, name="blogposts"),
path('posts/', views.posts, name="posts"),
After deploying my Blog website, let's say with the domain 12345xxx.com, where will I land?
You will get an error if you don't include a folder in the URL, as the URL patten won't be matched and the existance of non-admin URLs stops the default django page from showing. This will be either a 404 error or a 'Django tried these URL patterns, in this order:' type error if you have DEBUG=True on in settings.
Note that you don't have to provide a path (the path can be an empty string), and views can have multiple paths. In this case, perhaps
path('', views.home, name="home"),
path('home/', views.home, name="home_folder"),
path('blogposts/', views.blogposts, name="blogposts"),
path('posts/', views.posts, name="posts"),
would avoid an error.
On the administration page, I set the URL for the Django flat pages to "/", which is expected to be displayed as the home page at http://127.0.0.1:8000/. Doing so I encountered an error:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in core.urls, Django tried these URL patterns, in this order:
admin/
<path:url>
The empty path didn't match any of these.
But if I go to http://127.0.0.1:8000// with double slash then the home page is displayed correctly. My only urls.py file looks like this:
from django.contrib import admin
from django.urls import include, path
from django.contrib.flatpages import views
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += [
path('<path:url>', views.flatpage),
]
And I took all the code completely from the official guide. How to display the django flatpage homepage at http://127.0.0.1:8000/?
In addition to the django.urls.path method Django offers the django.urls.re_path method. The path method is designed to perform matches against exact strings, whereas the re_path method is designed to perform matches against patterned strings based on regular expressions. In my case it’s enough to fix it this way:
urlpatterns += [
re_path(r'^(?P<url>.*)$', views.flatpage),
]
As a result, we get the correct processing of requests at http://127.0.0.1:8000/. More details about using path, re_path methods can be found at the link.
I am creating a registration system with Django rest-auth and allauth. Looking at the documentation endpoints, I just used:
urls.py
app_name = "apis"
urlpatterns = [
path('users/', include('users.urls')),
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/customlogin', CustomLoginView.as_view(), name='rest_login'),
path('rest-auth/customlogout', CustomLogoutView.as_view(), name='rest_logout'),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
]
when I run, a browseable API appears like below:
After entering the details and post it, the below error occurs:
django.urls.exceptions.NoReverseMatch: Reverse for 'account_confirm_email' not found. 'account_confirm_email' is not a valid view function or pattern name.
is there another URL i need to implement (with the name of account_confirm_email) ?
I have read the documentations and rest-auth demos on this and it seems I need to include the following urls:
url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'),
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(), name='account_confirm_email'),
but this still did not fix the error why is that?
The problem was that I was adding this url:
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(), name='account_confirm_email'),
in the user app's urls.py rather than the project's urls.py. why is it that it cases the issue?
Using django-allauth, after a successful login a user is redirected to http://<myproject>/accounts/profile/... However this URL doesn't exists, but yet it still successfully shows view from http://<myproject>/profile/
settings.py
urlpatterns = [
path('', include('pages.urls')),
path('admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')),
url('album/', include('albums.urls')),
url('profile/', include('user_profile.urls')),
]
user_profile\urls.py
urlpatterns = [
path('', views.profile, name='profile'),
]
using show_urls I don't see anything for /accounts/* which would call the view.profile view
/accounts/confirm-email/ allauth.account.views.EmailVerificationSentView account_email_verification_sent
/accounts/confirm-email/<key>/ allauth.account.views.ConfirmEmailView account_confirm_email
/accounts/email/ allauth.account.views.EmailView account_email
/accounts/inactive/ allauth.account.views.AccountInactiveView account_inactive
/accounts/login/ allauth.account.views.LoginView account_login
/accounts/logout/ allauth.account.views.LogoutView account_logout
/accounts/password/change/ allauth.account.views.PasswordChangeView account_change_password
/accounts/password/reset/ allauth.account.views.PasswordResetView account_reset_password
/accounts/password/reset/done/ allauth.account.views.PasswordResetDoneView account_reset_password_done
/accounts/password/reset/key/<uidb36>-<key>/ allauth.account.views.PasswordResetFromKeyView account_reset_password_from_key
/accounts/password/reset/key/done/ allauth.account.views.PasswordResetFromKeyDoneView account_reset_password_from_key_done
/accounts/password/set/ allauth.account.views.PasswordSetView account_set_password
/accounts/signup/ allauth.account.views.SignupView account_signup
/accounts/social/connections/ allauth.socialaccount.views.ConnectionsView socialaccount_connections
/accounts/social/login/cancelled/ allauth.socialaccount.views.LoginCancelledView socialaccount_login_cancelled
/accounts/social/login/error/ allauth.socialaccount.views.LoginErrorView socialaccount_login_error
/accounts/social/signup/ allauth.socialaccount.views.SignupView socialaccount_signup
only the actual /profile/ url...
/profile/ user_profile.views.profile profile
help me to understand why /accounts/profile/ is showing the /profile/ view...
Actually redirecting to /accounts/profile/ is default behavior in django. In django global settings, it is defined as LOGIN_REDIRECT_URL. Django expects you to implement this page/url. Django django-allauth also uses same setting to redirect user after login.
If you want to redirect to any other page like /profile/, override this setting in your project settings.
LOGIN_REDIRECT_URL = '/profile/'
Or if you want django-allauth not to redirect at all set LOGIN_REDIRECT_URL = False in your settings as described here.
Your profile url path is not being limited to match only the start of the url:
url('profile/', include('user_profile.urls')),
This will match anything like gibberishprofile/ for example, including accounts/profile/. If you change the url config to
url('^profile/', include('user_profile.urls')), # note the ^ before profile
Then only profile/ will match.
I'm a full beginner on Django. Therefore, I'm sorry if my question is not making so much sense.
I'm studying Django tutorial - step 1. I installed properly Django and created my first Django project named 'vanilla'. The urls.py script of the vanilla project is
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
When I start Django with this 'vanilla' project and start the development server at http://127.0.0.1:8000/, I correctly see the start page (with a rocket).
In a second project that I named 'djtutorial', I created as requested in Django tutorial - step 1 a 'polls' app. As requested, I modified urls.py file in djtutorial\polls which now has following content:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
If I start the server the url http://127.0.0.1:8000/polls/ is working properly. However, if I launch the root url of the site (i.e. http://127.0.0.1:8000/), I now have a 404 error.
Why is the 'root url' of the site now hidden?
There wasn't any root URL at all. Main page informing you that your project is created properly shows up only if you don't have any view of your own added to urlpatterns. When you create any page, this welcome page stops showing up and that is expected.
If you want to show your own page on root of your website, use this in your urlpatterns:
path('', >>VIEW OR INCLUDE HERE<<),