I have a urls file that looks like this, however I cannot access the admin page. Is there any way to specify that the admin URL should never be overridden?
from django.contrib import admin
from django.urls import path
from latency import views
urlpatterns = [
path("admin/", admin.site.urls),
path("<str:chain>", views.regions),
path("<str:chain>/<str:region>", views.chain),
path("", views.index),
]```
I am keep getting 404 in DRF whenever I send request from postman even though the url is correct.
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('backend.urls'))
]
backend/urls.py
from django.urls import path, include
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register('/post', views.PostView, basename='post')
urlpatterns = [
path('', include(router.urls)),
]
From postman I am sending post request on the endpoint http://127.0.0.1:8000/post but I am keep getting 404 not found error. What am I doing wrong?
This is what works for me.
Create a app named backend using python manage.py startapp {app_name} where app_name is backend
Add the newly created app to INSTALLED_APPS in your project settings.py
In your project url.py file do this:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("backend.urls")),
]
In your app urls.py file. (where app is backend):
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register('post', views.PostView, basename='post')
app_name = 'backend'
urlpatterns = [
path('', include(router.urls)),
]
Then in your app(backend) views.py define PostView
This should work.The endpoint should be at :
http://localhost:8000/post/ .
if your using port 8000
Try to change backend/urls.py:
import views
from django.urls import path, include
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register('post', views.PostView, basename='post')
urlpatterns = router.urls
I have a HomeView Class which I added a login_required, but I don't know how to redirect it with the custom login page that I made.
Here are the urls.py and settings.py in my project:
myblog\urls.py
from django.contrib import admin
from django.urls import path,include
from .settings import DEBUG, STATIC_URL, STATIC_ROOT, MEDIA_URL, MEDIA_ROOT
from django.conf.urls.static import static
# from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myblog_app.urls')),
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
]
# + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
if DEBUG:
urlpatterns += static(STATIC_URL,document_root =STATIC_ROOT )
urlpatterns += static(MEDIA_URL,document_root = MEDIA_ROOT)
myblog_app\urls.py
from django.urls import path
from django.contrib.auth.decorators import login_required
from .views import HomeView,ArticleDetailView,AddPostView,UpdatePostView,DeletePostView,AddCategoryView,CategoryView,CategoryListView,LikeView,AddCommentView
urlpatterns = [
path('', login_required(HomeView.as_view()),name='home'),
path('article/<int:pk>', ArticleDetailView.as_view(),name='article-detail'),
path('add_post/', AddPostView.as_view(),name='add_post'),
path('article/<int:pk>/comment/', AddCommentView.as_view(),name='add_comment'),
path('article/edit/<int:pk>', UpdatePostView.as_view(),name='update_post'),
path('article/<int:pk>/remove', DeletePostView.as_view(),name='delete_post'),
path('add_category/', AddCategoryView.as_view(),name='add_category'),
path('category/<str:cats>', CategoryView,name='category'),
path('category-list', CategoryListView,name='category_list'),
path('like/<int:pk>', LikeView,name='like_post'),
]
members/urls.py
from django.urls import path
from .views import UserRegisterView,UserEditView,UserLoginView,PasswordsChangeView,ShowProfilePageView,EditProfilePageView
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('login/',UserLoginView, name='login'),
path('register/',UserRegisterView.as_view(), name='register'),
path('edit_profile/',UserEditView.as_view(), name='edit_profile'),
# path('password/',auth_views.PasswordChangeView.as_view(template_name='registration/change-password.html')),
path('password/',PasswordsChangeView.as_view(template_name='registration/change-password.html')),
path('password_success/',views.password_success,name='password_success'),
path('<int:pk>/profile/',ShowProfilePageView.as_view(),name='show_profile_page'),
path('<int:pk>/edit_profile_page/',EditProfilePageView.as_view(),name='edit_profile_page'),
]
settings.py
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL='home'
LOGOUT_REDIRECT_URL='login'
What it's doing now is that it basically just redirects to the
path('', login_required(HomeView.as_view()),name='home') and it doesn't know where to get the login url from, so instead it should redirect to path('login/',UserLoginView, name='login') but I have no idea how to do it
Okay I found the answer, so I just add in login_url='([login url path])' so that it redirects to the login url that I wanted... now it should look like this:
path('',login_required(HomeView.as_view(),login_url='/members/login/'),name='home'),
I've tried setting these URL's in this project, but I get a 404 when trying to access the registration.html file and the api-auth URL that is related to a REST API
For reference:
index = app
django_test = site
index/urls.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', views.homepage, name="homepage"),
path('/register', views.registration, name="registration"),
path('api', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace="rest framework")),
]
django_test/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('index.urls')),
path('admin/', admin.site.urls)
]
index/views.py
from django.shortcuts import render
def homepage(request):
return render(request,"home/homepage.html")
def registration(request):
return render(request, 'home/registration.html')
All of our HTML files are located in index/templates/home directory
Can you try changing name="registration" to name="register" ?
Change the URL "register/" to "/registration"
In views.py
from django.http import HttpResponse
def home_page(request):
return HttpResponse("<h1>Hello, World</h1>")
In urls.py
from django.contrib import admin
from django.urls import path
from .veiws import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
Starting development server at http://127.0.0.1:8000/
Your import says .veiws instead of .views