Multiple form classes in django generic (class) views

Facing similar problem, I’ve come to conclusion that it’s not possible. Though having multiple forms per page itself turned out to be a design mistake, presenting all sorts of troubles. E.g., user fills two forms, clicks submit on one of them and loses data from the other. Workaround requires complicated controller that needs to be … Read more

Django — Conditional Login Redirect

Create a separate view that redirects user’s based on whether they are in the admin group. from django.shortcuts import redirect def login_success(request): “”” Redirects users based on whether they are in the admins group “”” if request.user.groups.filter(name=”admins”).exists(): # user is an admin return redirect(“admin_list”) else: return redirect(“other_view”) Add the view to your urls.py, url(r’login_success/$’, views.login_success, … Read more

How to redirect from a view to another view In Django

As already suggested by @mdegis you can use the Django redirect function to redirect to another view or url. from django.shortcuts import redirect def view_to_redirect_to(request): #This could be the view that handles the display of created objects” …. perform action here return render(request, template, context) def my_view(request): …. perform form action here return redirect(view_to_redirect_to) Read … Read more

Forbidden (403) CSRF verification failed. Request aborted

You need to add {% csrf_token %} in your form https://docs.djangoproject.com/en/2.2/ref/csrf/ like that : <form> {% csrf_token %} <anything_else> </form> Also, you have to use RequestContext(request) everytime you use render_to_response : return render_to_response(“login.html”, {“registration_id”:registration_id}, context_instance=RequestContext(request)) And you have to import authenticate and login : from django.contrib.auth import authenticate, login

Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given

You need use as_view() at the end of class based views when declaring in the urls: path(”, views.HomeView.as_view(), name=”homepage”), Also, when using login_required decorator, you need to use it on dispatch method of CBV: from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator class HomeView(ListView): @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(HomeView, self).dispatch(*args, **kwargs)

Can I make STATICFILES_DIR same as STATIC_ROOT in Django 1.3?

No. In fact, the file django/contrib/staticfiles/finders.py even checks for this and raises an ImproperlyConfigured exception when you do so: “The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting” The STATICFILES_DIRS can contain other directories (not necessarily app directories) with static files and these static files will be collected into your STATIC_ROOT when you run collectstatic. … Read more

Django DoesNotExist

I have found the solution to this issue using ObjectDoesNotExist on this way from django.core.exceptions import ObjectDoesNotExist …… try: # try something except ObjectDoesNotExist: # do something After this, my code works as I need Thanks any way, your post help me to solve my issue

How organize pagination with a large number of pages in Django project?

I change pagination.html. Please try this. <nav aria-label=”pagination” class=”pagination_area”> <div class=”row”> {% if page.end_index > 0 %} <div class=”col-sm-12 col-md-5 d-none d-md-block”> <p>Showing {{ page.start_index }} to {{ page.end_index }} of {{ page.paginator.count}}.</p> </div> {% endif %} {% if page.paginator.num_pages > 1 %} <div class=”col-sm-12 col-md-7 dataTables_pager”> <ul class=”pagination”> {% if page.has_previous %} <li class=”page-item”> … Read more