Django resolve() doesn't resolve a valid path - python
I want to run test in Django, and I have a very simple test case that aims to test whether an url is reachable. I am using a valid path, but I still get a Resolver404 error. I don't know if it has something to do with multilanguage settings, or something.
This is my main urls.py:
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
from django.urls import path, include
from rest_framework.authtoken import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = i18n_patterns(
path('api-token-auth/', views.obtain_auth_token),
path('', include('FrontEndApp.urls')),
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('rosetta/', include('rosetta.urls')),
path('general/', include('GeneralApp.urls')),
#path('invoices_manager/', include('InvoicesManagerApp.urls')),
path('operations_manager/', include('OperationsManagerApp.urls')),
#path('payments_manager/', include('PaymentsManagerApp.urls')),
#path('providers_manager/', include('ProvidersManagerApp.urls')),
path('rates_manager/', include('RatesManagerApp.urls')),
#path('reports_manager/', include('ReportsManagerApp.urls')),
path('reservations_manager/', include('ReservationsManagerApp.urls')),
path('users_manager/', include('UsersManagerApp.urls'))
)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is my app urls.py:
from django.urls import path, include
from GeneralApp import views, models
from rest_framework import routers, renderers
APP_NAME = 'GeneralApp'
urlpatterns = [
path('', include(router.urls)),
path('catalogs/', views.CatalogViewSet.as_view({'get':'list'})),
path('countries/', countries_list, name='rest_countries_list'),
path('countries/<int:pk>/', country_detail, name='rest_country_detail'),
path('countries/<int:pk>/states/', states_list, name='rest_country_states_list'),
path('states/', states_list, name='rest_states_list'),
path('states/<int:pk>/', state_detail, name='rest_state_detail'),
path('states/<int:pk>/cities/', cities_list, name='rest_state_cities_list'),
path('cities/', cities_list, name='rest_cities_list'),
path('cities/<int:pk>/', city_detail, name='rest_city_detail'),
path('cities/<int:pk_city>/zones/', city_zones_list, name='rest_city_zones_list'),
path('zones/<int:pk>/', zone_detail, name='rest_zone_detail'),
path('zones/', zones_list, name='rest_zones_list'),
path('airports/<int:pk_city>/airports/', city_airports_list, name='rest_city_airports_list'),
path('airports/', airports_list, name='rest_airports_list'),
path('airports/<int:pk>/', airport_detail, name='rest_airport_detail'),
path('languages/', languages_list, name='rest_languages_list'),
path('languages/<int:pk>/', language_detail, name='rest_language_detail'),
path('provider_types/', provider_types_list, name='rest_provider_types_list'),
path('provider_types/<int:pk>/', provider_type_detail, name='rest_provider_type_detail'),
path('companies/', companies_list, name='rest_companies_list'),
path('companies/<int:pk>/', company_detail, name='rest_company_detail'),
path('companies/<int:pk>/provider/', provider_detail, name='rest_provider_detail'),
path('providers/', providers_list, name='rest_providers_list'),
path('providers/<int:pk>/', provider_detail, name='rest_provider_detail'),
path('company_providers/', company_providers_list, name='rest_company_providers_list'),
path('company_providers/<int:pk>/', company_provider_detail, name='rest_company_provider_detail'),
path('contacts_hlkd/', contacts_hlkd_list, name='rest_contacts_hlkd_list'),
path('contacts_hlkd/<int:pk>/', contact_hlkd_detail, name='rest_contact_hlkd_detail'),
path('contacts/', contacts_list, name='rest_contacts_list'),
path('contacts/<int:pk>/', contact_detail, name='rest_contact_detail'),
path('exchange_rates/', exchange_rates_list, name='rest_exchange_rates_list'),
path('exchange_rates/<int:pk>/', exchange_rate_detail, name='rest_exchage_rate_detail'),
path('zipcodes/', zipcodes_list, name='rest_zipcodes_list'),
path('zipcodes/<int:pk>/', zipcode_detail, name='rest_zipcode_detail'),
path('zipcodes/<int:pk>/cities/', cities_list, name='rest_cities_list'),
path('zipcodes/<int:pk>/states/', states_list, name='rest_states_list'),
path('zipcodes/<int:pk>/contries/', countries_list, name='rest_countries_list'),
path('markets/', markets_list, name='rest_markets_list'),
path('markets/<int:pk>/', market_detail, name='rest_market_detail'),
path('parameters/', parameters_list, name='rest_parameters_list'),
path('parameters/<int:pk>/', parameter_detail, name='rest_parameter_detail'),
]
And this is my tests.py:
from django.test import TestCase
from django.urls import resolve
from GeneralApp.views import CountryViewSet
# Create your tests here.
class CountryEndpointTest(TestCase):
def test_url_resolves_to_view(self):
found = resolve('/general/countries/')
self.assertEqual(found.func, views.CountryViewSet)
And this is the error I get:
File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/
urls/resolvers.py", line 523, in resolve
raise Resolver404({'tried': tried, 'path': new_path})
django.urls.exceptions.Resolver404: {'tried': [[<URLResolver <URLPattern list> (None:None) 'en-us/'>]], 'path': 'ge
neral/countries/'}
Pass prefix_default_language=False to the i18n_patterns function. If not, you must use the language code in all url patterns.
https://docs.djangoproject.com/en/2.0/topics/i18n/translation/#django.conf.urls.i18n.i18n_patterns
urlpatterns = i18n_patterns(
path('api-token-auth/', views.obtain_auth_token),
...
prefix_default_language=False
)
Related
Running Django Server
I need help tried to run server for Django and got this error, The included URLconf '<module 'myapp.urls' from 'C:\Users\user\Documents\Django Tutorial\myproject\myapp\urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import. for urls.py in myproject is: from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('myapp.urls')), ] for myapp views.py is: from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse('hello') For myapp urls.py from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
How to redirect CBV login_required to a specific route?
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'),
Error message in Django when I try to runserver - does not appear to have any patterns in it. If you s ee valid patterns in the
Every time I write python manage.py run server. I get the error message: The included URLconf '<module 'strana.views' from 'D:\\novi projekat\\strana\\views.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import. mysite urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('strana/', include('strana.views')), ] myapp(named strana) from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ] views.py from django.http import HttpResponse def index(request): return HttpResponse('Kita u Bila')
You included your views module. You want to include the module that contains your urls. from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('strana/', include('strana.urls')), ]
Using the URLconf defined in products.urls, Django tried these URL patterns, in this order:
*from django.urls import path, include from django.contrib import admin from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), path('products/',include('products.urls')), path('accounts/',include('accounts.urls')), ]* The above code of pyshop/url.py I could not login to admin page. from django.urls import path from . import views urlpatterns = [ path('', views.index), path('products/new',views.new), path('products/',views.products), path('accounts/register',views.register), ] The code is of product/urls.py.
Old question, but I run into the same problem, so just in case I can help someone else. I would put in the pyshop/url.py, this code (see the admin path is the only one with an actual url): from django.urls import path, include from django.contrib import admin from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), path('',include('products.urls')), path('',include('accounts.urls')), ] Then in your product/urls.py (See all of the routes end with a slash): from django.urls import path from . import views urlpatterns = [ path('', views.index, name="product"), path('products/new/',views.new, name="new_product"), path('products/',views.products, name="products"), path('accounts/register/',views.register, name="register"), ]
url, path in urlpatterns Django is not working
path('',include('personal.urls')), ^ SyntaxError: invalid syntax When I run the server using python manage.py runserver for the following code I got this error. url patterns from django.contrib import admin from django.urls import include from django.conf.urls import url from django.urls import path #from django.conf.urls import url #from django.urls import path urlpatterns = [ url('admin/', admin.site.urls) path('',include('personal.urls')), ] url patterns for my app 'personal' from django.urls import include from django.conf.urls import url from django.urls import path from . import views #import URLs urlpatterns = [ path('', views.index, name='index'), ] This is views.py code from django.shortcuts import render # Create your views here. def index(request): return render(request,'personal/home.html')
It's the comma after the first line. Replace with this: urlpatterns = [ url('admin/', admin.site.urls), path('',include('personal.urls')), ]
It might be due to missing namespace. Try to include the namespace in your main.urls. path('', include('personal.urls', namespace="personal")),