Why can't I add data to models in Django database? - django

I can't data to any of the schemas on my database. It gives error HTTP 400 when I click on save. Applicable to all schemas and models.
Images attached.
I expect it to add the data to the model but it says that the page isn't working and returns error 400.
And also, I created another Django project and I was able to add users there. I matched the settings.py file for both the projects and I couldn't spot any difference. Is there some place else I should be looking?
urls.py ->
from django.conf.urls import include, url
from django.contrib import admin
from django.http import HttpResponse
admin.autodiscover()
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^chat/', include('chat.urls', namespace='chat')),
]
No error in the console log
[2019/05/27 08:33:58] HTTP GET /admin/login/ 302 [0.04, 127.0.0.1:39500]
[2019/05/27 08:33:58] HTTP GET /admin/ 200 [0.05, 127.0.0.1:39500]
[2019/05/27 08:34:00] HTTP GET /admin/login/rooms/add/ 200 [0.04, 127.0.0.1:39500]
[2019/05/27 08:34:00] HTTP GET /admin/jsi18n/ 200 [0.03, 127.0.0.1:39500]
Repository link-
https://github.com/AdityaAjay/Collabgator

Related

Django - change answer returned when a page doesn't exist or user is not authenticated

I use Django in production.
I see that if an unauthenticated user accesses an exposed API - the server will return HTTP 401 Unauthorized.
If the user will access a non-existing API (with or without authentication) - the server will return 404 Not found.
This seems to me like a bad security practice and allows an attacker to find the server's exposed APIs.
Is there a way to change that so both will return the exact same result (I think 401 is the best practice, no?)
I would create a fall-back URL in the Django server to match all non-definitive URLs
from django.contrib import admin
from django.http.response import JsonResponse
from django.urls import path, re_path
def not_found_json(request, any_path=None):
return JsonResponse({'message': 'not allowed'}, status=403)
urlpatterns = [
path('admin/', admin.site.urls),
# app1 urls
# app2 urls
# app3 urls
# at last,
re_path(r'(?P<any_path>.+)', not_found_json, name='not-found-json'),
]
Notes
this fall-back URL definition must be in your ROOT_URLCONF--(Django doc)
The pattern expression must be on the bottom of the list
Use 403 Forbidden, which is more appropriate than 401 Unauthorized

Django redirecting from unknown reason

I was working on this app some time back and I have returned and for the life of me cannot see where a redirection is occurring.
From starting the app as below and navigating to the base URL, you can see attempt to reach '/Sites/' url
python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
June 20, 2018 - 16:35:05
Django version 1.11.13, using settings 'SysMan.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Not Found: /Sites/
[20/Jun/2018 16:35:07] "GET /Sites/ HTTP/1.1" 404 2263
The urls file is as below.
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic.base import RedirectView
from django.contrib.auth import views as auth_views
admin.site.site_header = 'Rio System Management'
admin.site.site_title = 'Rio SysMan admin'
admin.site.index_title = 'SysMan Administration'
urlpatterns = [
url(r'^$', RedirectView.as_view(url='/main/', permanent=True)),
url(r'^admin/', admin.site.urls),
url(r'^main/', include('apps.main.urls')),
url(r'^asnreg/', include('apps.asnreg.urls')),
]
I have run grep against the project folders for Sites with nothing of meaning being returned there.
Manually browsing to the URL's as below get me where I need to be.
http://127.0.0.1:8000/admin/
http://127.0.0.1:8000/main/
http://127.0.0.1:8000/asnreg/
Just can't figure why
http://127.0.0.1:8000/
redirects me to
http://127.0.0.1:8000/Sites/
I have looked at the various views file with nothing obvious there either
I did copy/paste the folders into a new virtenv. Could something be lingering in the DB??

Django 1.9 broken redirect after custom 404 page

On Django 1.9 multilingual site domain.com/ used to redirect to domain.com/en/ automatically.
Now, when I put a custom 404.html in top level templates dir, Django no longer redirects to the language:
domain.com/ throws server error 500 instead of redirecting to domain.com/en/
How keep custom 404 error page and get no server error 500?
Code:
urls:
from django.conf.urls import url, include
from django.contrib import admin
from django.http import HttpResponse
from django.conf.urls.i18n import i18n_patterns
urlpatterns = [
url(r'^robots\.txt$', lambda r: HttpResponse(" ", content_type="text/plain"))
]
urlpatterns += i18n_patterns(
url(r'^admin/', admin.site.urls),
url(r'^rosetta/', include('rosetta.urls')),
url(r'^', include('mp.urls', namespace='mp', app_name='mp')),
)
Dirs:
- project_folder
- app
-- templates
--- app_name
---- all templates
--- 404.html (had to put higher than app subdir, for Django to see it)
- project
-- settings, etc
view:
class Main(View):
def get(self, request):
c = get_seo_stuff("home")
t = "app/home.html"
return render(request, t, c)
def post(self, request):
pass
Code is pretty simple and it used to work just before the 404.html commit.
Figured it out. It was a multisite Django installation, and when I added a custom 404.html in one app, Django tried showing it on other apps as well.
The 404.html contained urls and stuff, not resolvable from the other tenant apps, resulting in Server Error 500.
Solution: add custom 404 handlers or use neutral 404.html page.

Django isn't displaying pages after a 404 error

I am hosting my site at entworks.in
The site seems to be working fine until I try to get a 404 error like entworks.in/asdf <-- will give 404. After that when I try to load other pages, like entworks.in, it shows 404 for all pages.
Anyone has any idea what might be the problem? I am running it on nginx.
EDIT:
URLCONF
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet
from haystack.views import search_view_factory
import bootlog.views
from bootlog.views import BSearchView
urlpatterns = patterns('',
url(r'^about/', 'bootlog.views.profile_view', name='about'),
url(r'^e/', 'bootlog.views.engineering_view', name='engineering'),
url(r'^d/', 'bootlog.views.programming_view', name='programming'),
url(r'^s/',search_view_factory(
view_class=BSearchView,
template='bootlog/base.html',
searchqueryset = SearchQuerySet(),
form_class=ModelSearchForm
), name='haystack_search'),
url(r'^ckeditor/',include('ckeditor.urls')),
url(r'^$',bootlog.views.front_page_view),
url(r'^p/b(?P<blog_pk>\d+)p(?P<post_pk>\d+)/', bootlog.views.perma_post, name="blog-post"),
url(r'^rss/$', bootlog.views.LatestEntriesFeed(), ),
url(r'^all/',bootlog.views.html_render),
)
handler404='bootlog.views.view_404'
I am using gunicorn
EDIT 2: I was displaying the 404 page in wrong way. Hence the error. It was overwriting the main config.

Custom Django 404 error

I have a 404.html page, but in some cases I want to be able to send a json error message (for 404 and 500, etc.). I read the following page:
https://docs.djangoproject.com/en/dev/topics/http/views/#the-404-page-not-found-view
Is there any sort of example that shows the implementation? I have it in my urls.py but it's not being picked up in the event of an error.
This worked for me:
from django.conf.urls import patterns, include, url
from django.views.static import *
from django.conf import settings
from django.conf.urls.defaults import handler404, handler500
from app.views import error
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'app.views.home', name='home'),
)
handler404 = error.error_handler
handler500 = error.error_handler
You can make it do anything as you wish when going to that controller.
In addition to the previous answer, it is important to say that the views.py should return a HttpResponse with a 404 status in the http header. It is important to inform the search engines that the current page is a 404. Spammers sometimes creates lots of urls that could seem that would lead you to some place, but then serves you another content. They frequently make lots of different addresses serve you almost the exact same content. And because it is not user friendly, most SEO guide lines penalize that. So if you have lots of addresses showing the same pseudo-404 content, it could not look good to the crawling systems from the search websites. Because of that you want to make sure that the page you are serving as a custom 404 has a 404 status. So here it is a good way to go:
Into your application's urls.py add:
# Imports
from django.conf.urls.static import static
from django.conf.urls import handler404
from django.conf.urls import patterns, include, url
from yourapplication import views
##
# Handles the URLS calls
urlpatterns = patterns('',
# url(r'^$', include('app.homepage.urls')),
)
handler404 = views.error404
Into your application's views.py add:
# Imports
from django.shortcuts import render
from django.http import HttpResponse
from django.template import Context, loader
##
# Handle 404 Errors
# #param request WSGIRequest list with all HTTP Request
def error404(request):
# 1. Load models for this view
#from idgsupply.models import My404Method
# 2. Generate Content for this view
template = loader.get_template('404.htm')
context = Context({
'message': 'All: %s' % request,
})
# 3. Return Template for this view + Data
return HttpResponse(content=template.render(context), content_type='text/html; charset=utf-8', status=404)
The secret is in the last line: status=404
Hope it helped!
I look forward to see the community inputs to this approach. =)
Basics:
To define custom view for handling 404 errors, define in the URL config, a view for handler404, like handler404 = 'views.error404'
Apart from the basics, some things to note about (custom 404 views):
It will be enabled only in Debug=False mode.
And more ignored one, across most answers (and this this stuck my brains out).
The 404 view defaults to
django.views.defaults.page_not_found(request, exception, template_name='404.html')
Notice the parameter exception
This was causing a 404 to 500 redirect from within def get_exception_response(self, request, resolver, status_code, exception) function defined in core.handlers.base since it could not find the parameter exception