can only concatenate str (not "NoneType") to str fordibben = ['!', '"', '#', '$', '%',…
- User
can only concatenate str (not "NoneType") to str fordibben = ['!', '"', '#', '$', '%', '&', "'", '*', '+', ',', ':', ';', '<', '>', '=', '?', '[', ']', '{', '}', ' ', '|', '^', '`'] for i in fordibben: if i in url: return HttpResponseRedirect('/new?e=Invalid+URL&title=' + title + "&url=" + url + "&filepath=" + filepath) Site.objects.create(url=url, title=title, content=file.read().decode('utf-8'), author=username) return HttpResponseRedirect('/panel')
- Assistant
The error message you're seeing ("can only concatenate str (not "NoneType") to str") typically occurs when you're trying to concatenate a string with a
Nonevalue.In your code snippet, the issue might be with one of the variables (
title,url, orfilepath) beingNone. Make sure that these variables have valid values assigned to them before using them in the concatenation.You can add some print statements or use a debugger to check the values of these variables and make sure they are not
None. - User
я написал такой код в urls.py:
from django.contrib import admin
from django.urls import path, re_path from main.views import * from django.conf.urls import handler404
urlpatterns = [ # path('admin/', admin.site.urls), path('favicon.ico', favicon), path('auth', authView), path('auth/', authView), path('auth/register', registerView), path('auth/register/', registerView), path('register', registerredirect), path('register/', registerredirect), path('auth/login', loginView), path('auth/login/', loginView), path('login', loginredirect), path('login', loginredirect), path('auth/register/usernameexist', registerUsernameExist), path('auth/register/submit', register), path('auth/login/usernameexist', loginUsernameExist),
path('auth/login/password', loginPasswordCorrect), path('auth/login/submit', login), path('panel', panel), path('panel/', panel), path('new', newSiteView), path('new/', newSiteView),path('<url>', showSite), path('<url>/', showSite),]
handler404 = notfound handler404: def notfound(req, e): return render(req, "404.html", status=404) но выдает server error (500)
- Assistant
There seems to be an issue with your code. The
handler404function should be defined outside of theurlpatternslist, and it should accept theexceptionparameter instead ofe. Here's an updated version of your code:# from django.contrib import admin from django.urls import path from main.views import * from django.conf.urls import handler404 # Define your urlpatterns as before urlpatterns = [ # ... ] # Define the handler404 function def notfound(request, exception): return render(request, "404.html", status=404) # Set the handler404 to the notfound function handler404 = notfoundMake sure you have also imported the necessary modules (
render, etc.) from Django. Also, check if your "404.html" template exists in the specified location.Try making these changes and see if the server error (500) is resolved.
- User
изменил код на это:
from django.contrib import admin
from django.urls import path, re_path from main.views import * from django.conf.urls import handler404
urlpatterns = [ # path('admin/', admin.site.urls), path('favicon.ico', favicon), path('auth', authView), path('auth/', authView), path('auth/register', registerView), path('auth/register/', registerView), path('register', registerredirect), path('register/', registerredirect), path('auth/login', loginView), path('auth/login/', loginView), path('login', loginredirect), path('login', loginredirect), path('auth/register/usernameexist', registerUsernameExist), path('auth/register/submit', register), path('auth/login/usernameexist', loginUsernameExist),
path('auth/login/password', loginPasswordCorrect), path('auth/login/submit', login), path('panel', panel), path('panel/', panel), path('new', newSiteView), path('new/', newSiteView),path('<url>', showSite), path('<url>/', showSite),]
handler404 = 'main.views.notfound' но не помогло содержимое функции main.views.notfound: def notfound(req, e): return render(req, "404.html", status=404) я использую Django version 4.2.6
- Assistant
In Django version 4.2.x, the
handler404expects a string in the format"module_name.view_name". So you need to adjust the configuration accordingly. Update your code as follows:from django.urls import path, re_path from main.views import * from django.conf.urls import handler404 # Write your urlpatterns as before urlpatterns = [ # ... ] # Set the handler404 to the view function as a string handler404 = 'main.views.notfound'Please make sure that the
notfoundfunction is defined in theviews.pyfile within themainapp, and ensure that the "404.html" template exists in the correct location.After making these changes, the 404 error page should be rendered correctly.