No known parent package - django

I made an app called calc in the project mysite. I tried this code on urls page of polls app
from django.urls import path
from . import views
urlspattern = [
path(' ',views.index, name= 'index')]
Then I got an ImportError
File "c:\Users\Anmol\tomb\mysite\polls\urls.py", line 3, in
from . import views ImportError: attempted relative import
with no known parent package

Related

Importing from views directory

In my project I have an app called api where I keep my viewsets in a directory called views. I keep getting ModuleNotFoundError: No module named 'api.views.book'; 'api.views' is not a package where I import in my api/urls.py
api/urls.py
from rest_framework import routers
from api.views.book import BookViewset
router = routers.DefaultRouter()
router.register(r'books', BookViewset, 'books')
api_urls = router.urls
In my main urls.py I'm doing this:
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from api.urls import api_urls
urlpatterns = [
path('api', include(api_urls)),
path('admin/', admin.site.urls),
]
I didn't get an error until I imported api_urls
I should mention that api is included in INSTALLED_APPS
You need to include a file named __init__.py in a directory to make it a package, then you can import from it
In your case you need a file api/views/__init__.py

Improperly Configured urls.py during Django deployment (Django 2.1)

This is my first time deploying Django. My app runs fine locally, but when I deploy, I get this error:
ImproperlyConfigured at /admin/
The included URLconf module 'search.urls' from '/home/imeaytbc/myproject/search/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.
My urls.py file is exactly the same as the one run on my computer:
from django.urls import path
from . import views
app_name = 'search'
urlpatterns = [
path('', views.query_search, name='query_search'),
path('article/<int:ArticleID>/', views.article_detail, name='article_detail')
]
Is there anything I need to change in regards to deployment? All the changes I made to my files regarding deployment are about static and media file directories. What else do I need to change for deployment? As far as I am aware, I have uploaded all files to the hosting server and the app shouldn't be missing any file.
EDIT: added main urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('search/', include('search.urls')),]

ImportError: from django.urls import path is not working

What is the probelm ?I am getting lot of stress with this code.
MY CODE::::
from django.contrib import admin
from django.urls import path
from basicapp import views
urlpatterns = [
path('',views.index,name='index'),
path('admin/', admin.site.urls),
path('formpage/',views.form_name_view,name='form_name'),
]
PROBLEM///ERROR::::
from django.urls import path
ImportError: cannot import name path
django.urls.path is new in Django 2.0. Make sure you use Django 2.0 or if you have to stick to <2.0 use django.conf.urls.url.
Docs for path (2.0): https://docs.djangoproject.com/en/2.1/ref/urls/#path
Docs for url (<2.0): https://docs.djangoproject.com/en/1.11/ref/urls/#url
It helps to use an editor that manages imports for you like PyCharm or Visual Code or Vi with appropriate plugins or many other.

Can I make a folder of forms and views and use an __init__.py file like models and test?

I would like to keep my views and forms separate, just like you can with models. Are you able to make a directory of views and forms, and if so, what goes in the init.py files for each.
Update:
I made my folders, but I keep getting errors. Here's all my code info:
myproject structure (abbreviated):
myproject/
myproject/
name/
forms/
__init__.py
name_form.py
models/
__init__.py
name_model.py
urls.py
views/
__init__.py
name_view.py
models/init.py
from .name_model import Name
models/name_model.py
from django.db import models
from django.urls import reverse
class Name(models.Model):
...
forms/__init__.py and views/__init__.py are blank files.
urls.py
from django.urls import path
from name.views import name_view
app_name = 'name'
urlpatterns = [
path('', views.name_view, name='name-view'),
]
forms/name_form.py
from django.forms import ModelForm, TextInput
from name.models import Name
class NameForm(ModelForm):
class Meta:
model = Name
views/name_view.py
from django.shortcuts import render, redirect
from name.forms import NameForm
def name_view(request):
...
I run python3 manage.py makemigrations in the terminal and get:
/myproject/name/views/name_view.py", line 4, in <module>
from name.forms import NameForm
ImportError: cannot import name 'NameForm'
Thinking you can't make a ModelForm without a model, I run python3 manage.py migrate and get the same error.
I created a project to isolate this issue. Without the folders it worked, unless I messed up my original code trying to get this to work.
Just make a folder for your views and a folder for your forms wherever you want them to be and put an empty __init__.py file into each folder. The purpose of the __init__.py file is to tell python to treat the folder as a module. Then make your views.py file and your forms.py file in their respective directories and now you can do...
from myproject.path.to.views import MyView
from myproject.path.to.forms import MyForm
...as if it were any other module. Which it is.

ImportError: No module named defaults

I am using django 1.9 version and I wanted to implement ajax search in my application. In the documentation it is says to add the urls to the root url patterns.
url(r'^ajax_search/',include('ajax_search.urls')),`
Then I am getting an import error as follows:
File "/usr/local/lib/python2.7/dist-packages/django_ajax_search-1.5.1-py2.7.egg/ajax_search/urls.py", line 1, in <module>
from django.conf.urls.defaults import *
ImportError: No module named defaults
Can any one help me solve this issue?
django.conf.urls.defaults has been removed from Django 1.6 onwards.
django-ajax-search package was last updated in 2013. The package has not been updated for a long and will not work smoothly for Django 1.9
Either you can find another package or you can manually update it.
django.conf.urls.defaults is deprecated in Django 1.4, later removed in Django 1.6. Read this. And the package you are using has the urls not compatible with Django 1.9. According to the Django 1.9 documentation you should define your urls.py as,
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
url(r'^articles/([0-9]{4})/$', views.year_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]
UPDATE:
You can modify your urls.py as below to make this working,
from django.conf.urls import url, include
from ajax_search import views as as_views
ajax_search_urlpatterns = [
url(r'^xhr_search$','as_views.xhr_search'),
url(r'^search/', 'as_views.search'),
]
urlpatterns = [
url(r'^ajax_search/',include(ajax_search_urlpatterns)),
]