I have created a very basic Djangodb where I am trying to do a batch import of several thousand excel files that I have. In the process of researching the best way to database all of these files I found this: https://github.com/pstch/django-batchimport
I have read through the installation documentations, and on the last step of adding the urls to the url.py file I keep getting a 404 error for both my localhost/admin and localhost/batchimport.
Here is the code for url.py
from django.conf.urls import include, url, patterns
from django.contrib import admin
from batchimport import *
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'export.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^batchimport', include('batchimport.urls')),
)
and here is my code for urls.py in my batchimport folder.
from django.conf.urls import *
from views import ImportUploadView, ImportOptionsView, ImportRunView
urlpatterns = patterns('',
url(r'^upload/$',
ImportUploadView.as_view(),
name='batchimport_upload'),
url(r'^options/$',
ImportOptionsView.as_view(),
name='batchimport_options'),
url(r'^run/$',
ImportRunView.as_view(),
name='batchimport_run'), )
Here is the output from the error that I am getting:
ImportError at /admin/ cannot import name related
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.8
Exception Type: ImportError
Exception Value: cannot import name related
Exception Location: /Users/USER/Development/ExportOCC/export/batchimport/utils.py
in <module>, line 5 Python
Executable: /Users/USER/anaconda/bin/python Python
Version: 2.7.9
The import is not available in Django 1.8. You can give it a try and import related from the original source. Change batchimport/utils.py like:
from django.db.models import get_model
from django.db.models.fields import AutoField, related
If that works, please also notify the author of django-batchimport!
Related
Python: 3.6.0 | Django: 3.1.4
By going through the documentation, I created one app, below is my app url.py (from app) file
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
My site urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('npimgapp/', include('npimgapp.urls')),
path('admin/', admin.site.urls)
]
My view.py file in app
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Welcome ...")
On executing this code I am getting below error:
django.core.exceptions.ImproperlyConfigured: The included URLconf
'npimgpro.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 i mport.
Also, below is my directory structure for ref:
Your directory structure is wonky; there shouldn't be two nested npimgpro folders.
Move the contents of that npimgpro folder up one level, so it's
npimgpro/
(etc.)
npimgapp/
(etc.)
manage.py
db.sqlite3
I am a total beginner in "django" so I'm following some tutorials currently I' am watching https://youtu.be/JT80XhYJdBw Clever Programmer's tutorial which he follows django tutorial
Everything was cool until making a polls url
Code of views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
HttpResponse("Hello World.You're at the polls index")
Code of polls\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Code of Mypr\urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/',admin.site.urls),
path('polls/',include('polls.urls')),
]
I don't get it I did the same thing but I'm getting error not only polls.In one turtorial he decided to make blog,and again the same error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in
this order:
admin/
The current path, polls/, didn't match any of these.
Please my seniors help me.
Note:I'm using the latest versions of django,windows and as editor I'm using Pycharm.
Already tried(and did not work):
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file.
Change that to False, and Django will display a standard 404 page.
try to access polls/ URL in your browser then you will access the page
Because you have accessed the URL of your project, you have to go to this URL to access your app
try by changing your code like this
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
I was facing the same error. Feel kinda dumb after figuring out how to solve this.
Your urls.py content is all right
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
But it must be included in the mysite\mysite\urls.py and not in mysite\urls.py.
That means the inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it.
I was recently building a django project. I have been looking through the files for a couple of hours now and can't find the problem that would result in this kind of error message. Below, I will show you all of the relevant files within the project.
base url.py:
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^display/', include('diplay.urls')),
]
app url.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
app views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h2>HEY!</h2>")
I'm not sure why this is not working because I found a similar format online, and it seemed that every line was similar to the other one. When I try running the server, it gives me the error statement
ImportError: No module named diplay.urls
Any ideas?
1, make sure your app name is diplay which is same as in your base urls.py, I think maybe there is typo, should change to:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^display/', include('display.urls')),
]
2, make sure the file name of urls should be urls.py instead of url.py in both base and app folder
Does app diplay exists or is it typo of display
> diplay.urls
Also does urls file in app exists
New python/Django user (and indeed new to SO):
When trying to migrate my Django project, I get an error:
RemovedInDjango110Warning: Support for string view arguments to url() is deprecated
and will be removed in Django 1.10 (got main.views.home). Pass the callable instead.
url(r'^$', 'main.views.home')
Apparently the second argument can't be a string anymore. I came to create this code as it is through a tutorial at pluralsight.com that is teaching how to use Django with a previous version (I'm currently working with 1.9). The teacher instructs us to create urlpatterns in urls.py from the views we create in apps. He teaches us to create a urlpattern such as the following:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', 'main.views.home')
]
to reference
def home(request):
return render(request, "main/home.html",
{'message': 'You\'ve met with a terrible fate, haven\'t you?'}) #this message calls HTML, not shown, not important for question
in the views.py of an app "main" that I created.
If this method is being deprecated, how do I pass the view argument not as a string? If I just remove the quotes, as shown in the documentation (https://docs.djangoproject.com/en/1.9/topics/http/urls/), I get an error:
NameError: name 'main' is not defined
I tried to "import" views or main using the code presented in this documentation:
from . import views
or
from . import main
which gave me:
ImportError: cannot import name 'views'
and
ImportError: cannot import name 'main'
I believe I've traced this down to an import error, and am currently researching that.
I have found the answer to my question. It was indeed an import error. For Django 1.10, you now have to import the app's view.py, and then pass the second argument of url() without quotes. Here is my code now in urls.py:
from django.conf.urls import url
from django.contrib import admin
import main.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', main.views.home)
]
I did not change anything in the app or view.py files.
Props to #Rik Poggi for illustrating how to import in his answer to this question:
Django - Import views from separate apps
You should be able to use the following:
from django.conf.urls import url
from django.contrib import admin
from main import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home)
]
I'm not absolutely certain what your directory structure looks like, but using a relative import such as from . import X is for when the files are in the same folder as each other.
You can use your functions by importing all of them to list and added each one of them to urlpatterns.
from django.conf.urls import url
from django.contrib import admin
from main.views import(
home,
function2,
function3,
)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/$', home),
url(r'function2/^$', function2),
url(r'^$', function3),
]
I'm using the new i18n_patterns of Django 1.4:
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
admin.autodiscover()
urlpatterns += i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
)
It works for every active language:
/en/admin/ # Ok
/es/admin/ # Ok
But this fails:
/admin/ # 404 Not found
How to avoid the 404 error and redirect to a language-prefixed version of the requested URL (not only the admin panel)?
Is to write a custom middleware the solution? Why this doesn't come by default in Django?
It looks like you did not enable django.middleware.locale.LocaleMiddleware.