I have created Djnago project in eclipse. Unfortunately, i am facing issue when i run the project
ImportError at /
No module named urls
Here Error Page
http://dpaste.com/1499981/
Eclipse Project http://i1008.photobucket.com/albums/af204/shoaibshah01/Untitled_zps84f95b4f.jpg
urls.py Content
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'TestApp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Try converting admin.site.urls to string url(r'^admin/', include('admin.site.urls'))
Most likely this is because your TestApp is not referenced in INSTALLED_APPS so Django have no idea it should process it.
Try to change your ROOT_URLCONF to 'urls' value in settings.py
ROOT_URLCONF = 'urls'
The link you provided for the error log is giving 404.
Perhaps you can try with the below code for admin in URLs.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Everything is looking correct in settings.py.
If the above changes are not working, please share error page again.
Related
I am developing a website with Django, I had a lot of pictures in my project, uploaded from the admin panel and saved in the Media folder which I created for these uploads separately, It was working fine and exact way I wanted in months, Suddenly they are just not loading, getting 404 for all of them, without any change in project, they are just not loading.
My media path in Settings.py :
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
I have added this to the end of my urls.py of the app:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and as i said, it was working fine for a long, suddenly this happened
edit: I just figured it out that this is happening when I am using redirect function in one of my views
Another way to do it is:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# other URLs ...
]
# If DEBUG=True in the settings file
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
So we're adding static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to the urlpatterns if DEBUG=True in the settings.py file. This gives a better management in some sense.
I had the same issue and this helps me :
Add this in your urls.py in the urlpatterns list :
from django.views.static import serve
from django.conf import settings
from django.conf.urls import url
urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
# YOUR URLS ARE HERE
]
Of course stay in Debug=True in settings.py.
I am getting page not found(404) error for votings app that I created from datacamp tutorial. I have checked my code to make sure it's free of errors. admin is working fine but other urls are not.
Here's urls.py code from the main application directory:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Here's urls.py from the votings app directory:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
I am using django 2.0.5.
Thanks
Unless you've made a mistake copying the wrong urls.py for votings app, the problem has to be it.
This is the main urls.py of your project:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
FYI, according to the docs include() adds urls from your app directory's (in your case it's voting) urls.py to the main urls.py (in memory). This keeps the main urls.py from getting too big to read.
And this is the urls.py of your votings app which is literally the copy of main urls.py:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Don't you see any problem here? There's no endpoint. Where's the associated view (function-based or class based) for this url?
I suggest writing a view in your views.py and test it out:
Votings app views.py:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Votings app urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path('home/', views.current_datetime, name='home'),
]
I am trying to get django-debug-toolbar working. I have followed the installation steps and I get the sidebar including stats (e.g. SQL 1 query in 2.75ms, Static Files 19 Files used, 30 receivers of 12 signals) which seem to be legitimate and indicate that its working.
However, when I click for more info on a given tab, I get a 404 in browser, and this sort of thing in the console:
"GET /__debug__/render_panel/?store_id=ac74875cfe864b2dab4c6d17c1d1ed5d&panel_id=RequestPanel HTTP/1.1" 404 1791"
Other pages on site do work.
I have tried various configurations in urls.py. Here is what I currently have:
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailcore import urls as wagtail_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from search import views as search_views
urlpatterns = [
url(r'^django-admin/', include(admin.site.urls)),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r'', include(wagtail_urls)),
# Alternatively, if you want Wagtail pages to be served from a subpath
# of your site, rather than the site root:
# url(r'^pages/', include(wagtail_urls)),
]
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Serve static and media files from development server
# urlpatterns = [
# url(r'^__debug__/', include(debug_toolbar.urls)),
# ]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have tried a few other configurations here, including having:
import debug_toolbar
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
in the if settings.DEBUG: block.
Any ideas?
As the comment says, the wagtail urls must be the last pattern.
One option would be to move the debug toolbar urls to the beginning of the list:
urlpatterns = [
...
]
if settings.DEBUG:
urlpatterns = [
url(r'^__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
Or you could remove the wagtail urls from there current position, and add them after your if settings.DEBUG: block.
if settings.DEBUG:
urlpatterns += [
url(r'^__debug__/', include(debug_toolbar.urls)),
]
urlpatterns += [
url(r'', include(wagtail_urls)),
]
I’ve included the debug toolbar urls inside and if settings.DEBUG: block here because that’s what the docs recommend, but that’s not the reason why it works. The key is to make sure that the wagtail urls come at the very end.
I don't know what I changed in previous django project when I run any new project without setting any thing in urlpatterns it shows
error
instead of original Django running
I already tried uninstalling pycharm to deleting the project nothing works
urls.py is
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),]
views.py
from django.shortcuts import render
I used this piece of code in previous project
urlpatterns = [
path('admin/', admin.site.urls),
path('catalog/', include('catalog.urls')),
path('', RedirectView.as_view(url='/catalog/', permanent=True)),] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I know it's feels like elementary, and yet I can't come up with a clean solution based on doc only.
I have the following project structure (I omit files like models.py, forms.py for the purpose of keeping the question concise)
hello_world
hellow_world
urls.py
app_2
urls.py
app_3
urls.py
manage.py
urls.py
settings.py
As you see, my goal is to have a separate urls.py file for each app, and then assemble them into root urls.py (depicted at the same level as settings.py in the list above). The problem is that my root urls.py is EMPTY (!!!) now, and the site still loads the home page !!! What am I doing wrong ???
See the details below:
settings.py:
ROOT_URLCONF = 'urls'
hellow_world urls.py:
urlpatterns = [
url(r'^$', views.home , name = 'home'),
]
root urls.py - empty !
manage.py:
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Use include() to include more urls:
# your main urls.py file
from django.conf.urls import include, url
urlpatterns = [
url(r'^myapp1/', include('myapp1.urls')),
url(r'^myapp2/', include('myapp2.urls')),
]
And:
# myapp1/urls.py
from django.conf.urls import url
from . import views
app_name = 'myapp1'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
#...
]