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)
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 got a 404 error on my website when accessing the correct route, did I do something wrong
urls.py handles the main route
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
urls.py handles routes in the apifacebooks app
from django.urls import path
from . import views
urlpatterns = [
path('api/facebook/like-post',views.like_post)
]
And when I go to http://localhost:8000/api/facebook/like-post I get a 404 error
Image error 404
My question has been solved, thanks
In your apifacebooks app change the path because you had "api..." double in the path
from django.urls import path
from . import views
urlpatterns = [
path('like-post/',views.like_post)
]
And in the root urls.py just
path('api/facebook/', ....)
In your code, the url pattern would be "http://localhost:8000/api/facebook/api/facebook/like-post".
As explained id Django docs:
Whenever Django encounters include(), it chops off whatever part of
the URL matched up to that point and sends the remaining string to the
included URLconf for further processing.
Remove "api/facebook/" in "apifacebooks.urls", for example:
Main urls.py
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'api/facebook/(.+?)/', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
apifacebooks.urls
from django.urls import path
from . import views
urlpatterns = [
path('like-post',views.like_post)
]
Or you can try to place remove "r'api/facebook/(.+?)'", for example:
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('apifacebooks.urls')),
path('', include('app.urls')),
path('getcookie', include('app.urls')),
path('change-lang', include('app.urls')),
re_path(r'auth/(.+?)', include('app.urls')),
]
I have a Django projects with two apps, "projects" and "codebox", they were running fine, then at some point I got the following error:
django.urls.exceptions.NoReverseMatch: 'admin' is not a registered
namespace
If I remove the link to my admin panel from my template this error goes away, but then I can't get to my admin panel:
Admin
I was working in the urls.py files when this error occurred, have I changed something that is inadvertently having an impact on the admin link?
Here is my top level urls.py file:
from django.contrib import admin
from django.urls import include, path, re_path
urlpatterns = [
# path('', include('projects.urls')),
path('projects/', include('projects.urls')),
re_path('^accounts/', include('django.contrib.auth.urls')),
re_path('^logged_out/', include('projects.urls')),
path('codebox/', include('codebox.urls')),
]
Here is my urls.py for "projects":
from django.urls import path, include
from . import views
app_name = 'projects'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('insight/', views.InsightView.as_view(), name='insight'),
path('logged_out/', views.LoggedoutView.as_view(), name='loggedout'),
path('insight/subgen/', views.SubgenView.as_view(), name='subgen'),
]
And here is urls.py for my second app, codebox:
from django.urls import path, include
from . import views
app_name = 'codebox'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('form/', views.CreateView, name="form"),
]
add this in top of project/urls.py
from django.conf.urls import include, url
then add this in the url_pattern
url(r' ', include(('<app_name>.urls','<app_name>'), namespace='<app_name>')),
hope this will work
Check your base html file which you're extending to other html pages. If there is some space between urls tag remove that space and save your file and try again you can check below code for ref.
from django.contrib import admin
from django.urls import path, include
from passapp import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('passapp/', include('passapp.urls')),
]
href="{% url 'admin:index' %}"
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 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.