I'm trying to import 'views' file to my 'urls' so I could map the path to this 'vehicle_validation' function.
For some reason, pycharm can't find this file.
can someone help me understand what's the problem?
urls file:
from django.urls import path
from vehicule_approver import views # error here
urlpatterns = [
path('admin/', admin.site.urls),
path('vehicle_validation/', views.vehicle_validation)
]
views file:
import requests
from django.http import HttpResponse
import json
from pyapi.vehicule_approver.models import Vehicle
def vehicle_validation(request):
...
project structure:
structure image
I think the problem cames from the import of vehicule_approver you used in views.py.
With from vehicule_approver import views Python tries to find a module vehicule_approver inside your sys.path.
Perhaps use one of those imports:
from ..vehicule_approver import views # Relative import
from pyapi.vehicule_approver import views # Absoute import
The problem is:
from pyapi.vehicule_approver.models import Vehicle
Imports should be relative to the project directory (the one that contains manage.py), so you should remove the pyapi and change the import to:
from vehicule_approver.models import Vehicle
Alternatively, since you are importing from the same app, you can use a relative import.
from .models import Vehicle
Related
I have a Django project containing some files which are, obviously, not automatically discovered by Django. My workaround is to import them in urls.py so that Django can see them. This is how my urls.py looks like:
from django.contrib import admin
from django.urls import path
from custom_file_1 import * # "unused" import
from custom_file_2 import * # "unused" import
urlpatterns = [
...
]
My IDE considers the commented imports unused, since they are not used, but they are vital so that Django can process those files.
And the question: is there any nice way to let Django see those files? And how to do that?
It is usually not a good idea to import things with wildcards. Imagine that in one of the custom files some object with the name path is present, then it will override the reference to the path function you imported from django.urls.
Usually one imports such files (that for example contain signals, etc.) in the AppConfig.
In the directory of the app, there is an __init__.py file. You can write
# app/__init__.py
default_app_config = 'app.config.CustomAppConfig'
In your app directory, you then define the config of your app:
# app/config.py
from django.apps import AppConfig
class CustomAppConfig(AppConfig):
name = 'app'
def ready(self):
import custom_file_1 # noqa
import custom_file_2 # noqa
Here # noqa is used to ignore the flake8 warnings.
I have created a simple webapp with django to display some lists from a .csv file. This program works perfectly fine on my local machine using runserver, however, when attempting to deploy using DigitalOcean using the following guide: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04#create-a-gunicorn-systemd-service-file , when I run ~//manage.py runserver 0.0.0.0:8000 I am greeted with a
TypeError: 'module' object is not iterable
followed by
"ImproperlyConfigured: The included URLconf .urls' 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."
After spending the day searching around the majority of other people with this problem have spelling mistakes or missing commas however I don't THINK that's the case for me. I've shown my url files below:
urls.py - project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('recommendation/', include('recommendation.urls')),
path('admin/', admin.site.urls),
]
urls.py - recommendation
from django.urls import path
from django.conf.urls import url
from . import views
app_name = 'recommendation'
urlpatterns = [
path('', views.index, name='index'),
url(r'^movie/search', views.movie, name='movie'),
]
I saw a post explaining that the 'module' object was being found due to urlpatterns not being properly defined as lists (missing commas).
Any help is much appreciated, thanks for your time and any advice
EDIT: Image of traceback
screencap of traceback from putty terminal
EDIT 2: To give an idea of how mmy views and "engine" file operate in terms of imports and functions:
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.http import Http404
from django.views.decorators.csrf import csrf_exempt
import engine as eng
import urllib.request
import urllib.parse
import re
import json
def index(request):
lom = eng.movielist
return render(request, 'recommendation/index.html', {'lom':lom})
#csrf_exempt
def movie(request):
handles GET requests, returns a list of movie info + youtube trailers
engine.py
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
from ast import literal_eval
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
//the code for processing a movie choice and returning a list of recommendatioons
FINAL EDIT: the problem was that I didnt have sklearn installed on my server...whoops
I'm changing view of homepage with app names pages.
I've added pages to settings. this is how directory look like:
- trydjango
- src/
- pages/
- __init__
- views
- products
- trydjango/
- __init__
- settings
- urls
- manage
views' code:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_view(*args, **kwargs):
return HttpResponse("<h1>Hello Again</h1>")
urls code
from django.contrib import admin
from django.urls import path
from src.pages.views import home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='home'),
]
and I see this error when I run server
ModuleNotFoundError: No module named 'src'
First you need to understand what an app in Django is compared to a project.
When you register an app django will look in the project root folder when you try to import it.
Your project root is where your manage.py file is. In your case the src folder.
So when you want to import your views module you need to state
from pages.views
rather than
from src.pages.views
I suggest that you read through and follow (by coding it yourself) the Django tutorial to learn more about project structure and creating your own apps with models, urls etc.
I got the same problem, IDE may use red underline but this code is still correct:
from pages.views
I've looked at just about everything I can here. I'm using python 3.5 and i've seen stuff about how they've changed the way imports work. My django project structure is like this:
project
--app
--views/
--__init__.py
--myFile.py
--__init__.py
--models.py
--admin.py
--urls.py
--etc....
My urls.py is such:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^api$', views.function_from_myFile_that_is_not_being_found),
otherurls()...
]
The error I'm getting is AttributeError: module 'app.views' has no attribute 'function_from_myFile_that_is_not_being_found'
I'm really lost as to why this is happening. I've tried putting imports in my __init__.py files and that hasn't worked either. Not sure what else I'm missing.
Thanks in advance.
In your project views is a package, not a single module. So you should do
from .views import myFile as views
Following the django-rest tutorial
app/urls.py:
from django.conf.urls import url, include
from rest_framework import routers
from app.abbr import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Directory structure:
Error:
File "..../app/app/urls.py", line 3, in
from app.abbr import views
ImportError: No module named 'app.abbr'
So, sigh...
It would have been useful if you pointed to the tutorial that showed you to do this.
You should not import from app; that refers to the inner directory containing your urls.py. Just import from abbr.
from abbr import views
And what if you change import like this?
from app.app.abbr import views?
I am considering that you are using django 1.9 +
Try this
from . import views
The root directory folder named App in your case is named after your project name by default when you start a new project via the django-admin startproject command.
you can rename your root directory folder to whatever you want and it won't affect your project.
when in your code are importing from app, it is actually looking inside the 'app' folder containig the 'settings.py' file.
the django-rest tutorial you are following contains an error when they are doing from tutorial.quickstart import views which should be from quickstart import views
so the same goes for you, you should do from abbr import views