import models from another directory - python

I'm new in Django.
i am following a tutorial and now i have a problem .
my directory tree is like
and in /pages/views i have this
from django.http import HttpResponse
from django.shortcuts import render
def home_view(*args, **kwargs):
return "<h1>Hello world</h1>"
and in trydjango/urls i have this
from django.contrib import admin
from django.urls import path
from pages.views import home_view
urlpatterns = [
path('', home_view, name='home'),
path('admin/', admin.site.urls),
]
***but it can't recognize pages ***
i also tried these
solution
but didn't work!what do i missing?
even i added these codes to settings.py
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(BASE_DIR, 'pages'))
{ django 3.1 }

It looks like you forgot to create a urls.py file inside the 'pages' app.
in trydjango/urls.py you have to include the urls of the app:
urlpatterns = [
path('pages/', include('pages.urls'), name="pages"),
path('admin/', admin.site.urls),
]
Then create the pages/urls.py file and it should work:
urlpatterns = [
path('', home_view, name='home'),
]
note; dont forget to add 'pages' in the installed_apps inside your settings.py file.

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'),
]

I still get You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs

thats my app urls
from django.urls import path
from . import views
urlpatterns = [
path('', views.tasklist, name='tasks'),
]
thats my projects urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('list.urls')),
]
thats views urls
from django.shortcuts import render
from django.http import HttpResponse
def tasklist(request):
return HttpResponse('TO DO List')
I tried a lot of things but none of them worked
you have to add your app into INSTALLED_APPS list in settings.py

How do I fix the circular import in django

I am trying to simply put a 'Hello World' text on the server. And it brings an error.
Project Urls.py :
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls'))
]
App Urls.py
from django.urls import path
from . import views
urlpattern = [
path('', views.index,name = 'index')
]
Views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(response):
return HttpResponse('<h1>Hello World</h1>')
The Error it tells me:
The included URLconf in myapp/urls.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.
You are missing an 's' in your apps urls.py.
it has to be 'urlpatterns', instead of 'urlpattern'
urlpatterns = [
path('', views.index,name = 'index')
]

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"),
]

Django resolve() doesn't resolve a valid path

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
)

Categories