server – Understanding the importance of Gunicorn and Nginx for Django web development

When author says that Django can’t handle real workloads, they most likely refer specifically to Django’s built-in webserver.

When you run Django in development environment, you can use it to test things out, but a proper webserver is a fairly complex piece of software and requires very specific competences to build. Moreover, there is no real reason for the people behind Django to also spend time making a fully fleshed-out webserver: it is much easier to build an interface for an existing one.

Nginx is a professionally made workhorse of a server. It is feature-rich and is built to handle production workloads. Most apps use a server like that on the front to handle niche responsibilities like caching, redirection, forming headers, compression etc.

So, now, Gunicorn. Gunicorn is a WSGI server. Broadly speaking, it facilitates communication between a variety of webservers and a variety of frameworks through a standardized interface. Plus it handles additional tasks, like load-balancing and maintaining instances of your app alive, so you don’t have to.

ValueError: demasiados valores para descomprimir (esperado 2) Django

Tengo un problema en mi proyecto ‘Django’ y necesito ayuda con este error, el error ocurre cuando quiero crear migraciones, uso PyCharm para trabajar y a la vez uso la consola de cuya IDE, cuando ejecuto la creación de la migración “python manage.py makemigrations” me sale todo este error:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesdjangocoremanagement__init__.py", line 401, in exec
ute_from_command_line
    utility.execute()
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesdjangocoremanagement__init__.py", line 377, in exec
ute
    django.setup()
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesdjango__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesdjangoappsregistry.py", line 122, in populate
    app_config.ready()
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesdjangocontribadminapps.py", line 24, in ready
    self.module.autodiscover()
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesdjangocontribadmin__init__.py", line 24, in autodis
cover
    autodiscover_modules('admin', register_to=site)
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesdjangoutilsmodule_loading.py", line 47, in autodisco
ver_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "c:userscirciappdatalocalprogramspythonpython38libimportlib__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name(level:), package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:UserscirciDocumentsDjangoGout.ioGoutAppadmin.py", line 2, in <module>
    from .models.product import Product
  File "C:UserscirciDocumentsDjangoGout.ioGoutAppmodelsproduct.py", line 34, in <module>
    class Product(models.Model):
  File "C:UserscirciDocumentsDjangoGout.ioGoutAppmodelsproduct.py", line 40, in Product
    choiceGameCategory = MultiSelectField(choices=GameCategory)
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesmultiselectfielddbfields.py", line 71, in __init__
    self.max_length = get_max_length(self.choices, self.max_length)
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesmultiselectfieldutils.py", line 31, in get_max_length

    return len(','.join((string_type(key) for key, label in choices)))
  File "C:UserscirciDocumentsDjangoGout.iovelibsite-packagesmultiselectfieldutils.py", line 31, in <listcomp>
    return len(','.join((string_type(key) for key, label in choices)))
ValueError: too many values to unpack (expected 2)

Este fracmento muestra como tengo el código en el archivo urls.py:

from django.contrib import admin
from django.urls import path
from django.urls import path, include
from django.conf.urls.static import static
from . import settings


urlpatterns = (
    path('admin/', admin.site.urls),
    path('', include('GoutApp.urls'))
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Este fracmento muestra como tengo el código en el archivo admin.py:

from django.contrib import admin
from .models.product import Product


class AdminProduct(admin.ModelAdmin):
    list_display = ('name', 'Pricing', 'choicePrice', 'choiceApp', 'choiceGameCategory')


# Register your models here.
admin.site.register(Product, AdminProduct)

La manera en que el directorio está organizada es de la siguente forma:

Así tengo mi directorio:

server – Understanding the importance of Gunicorn and Nginx for Django web development

I’m entirely uninitiated to the world of web development, and only have a tentative grasp on Django and web development through the test server it works through.

From the guide I’m reading, the author turns to using Nginx once he starts working on site deployment, because Django is “not designed for real-life workloads.” What does that mean, and why doesn’t it? In terms of justification for using Gunicorn, the author remarks:

Do you know why the Django mascot is a pony? The story is that Django
comes with so many things you want: an ORM, all sorts of middleware,
the admin site…​ “What else do you want, a pony?” Well, Gunicorn
stands for “Green Unicorn”, which I guess is what you’d want next if
you already had a pony…​

Well and good, but I don’t really know what the two are doing for the server. I know for web developers this is like asking what multiplication is to a maths professor, so please excuse the naivety. In your please keep in mind I have almost no knowledge of web development other than what I’ve thus far learned from this guide, doing my best to understand as much as I can for the previously entirely uninitiated (I’m from a computational programming background).

Get user and users of ancestors of a model instance if it has `organizationmember` attribute or else only user of the instance in Django

I have the following models:

class Client(BaseModel):
    #other fields
    engineers = models.ManyToManyField(Engineer, null = True, blank = True, related_name='clients')

class OrganizationMember(MPTTModel):
    designation = models.CharField(max_length = 100, blank = True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', on_delete=models.SET_NULL)

Now I would like to get all engineers of a client instance and check if each engineer has an organizationmember instance or not.
Then if they belong to some organization then I should get_ancestors of that organizationmember and then the users.
If they do not have any organizationmember then just get the user of that engineer instance.

For this I have written a method in Client model as below:

class Client(BaseModel):

    def get_users_from_engineers(self):
        users_in_organization = ()
        users_outside_organization = ()

        engineers = self.engineers.all()
        if engineers:
            for engineer in engineers:
                user = engineer.user
                if hasattr(user, "organizationmember"):
                    users_in_organization += (orgmember.user for orgmember in user.organizationmember.get_ancestors(include_self=True))
                else:
                    users_outside_organization.append(user)

Now this code is working correctly but I am sure that I can use django filters here instead of two for loops and optimize this code. But I do not know how.

For eg:

OrganizationMember.objects.filter(user__engineer__in=self.engineers.all())

can be used to get all the organizationmembers of the engineers of that particular client instance. But I need the users of those organizationmembers.
Also if I use get_ancestors on multiple engineers then more than one engineer might have the same ancestor thereby adding the same person more than once in the users_in_organization list. How can I rectify this if I use django filters?

Edit:

In the method get_users_from_engineers I can also use as follows:

users_outside_organization = list(User.objects.filter(engineer__in=engineers, organizationmember__isnull=True)

But I do not know how to get the users_in_organization values as stated above using django orm methods.

Note: This is my first question in codereview. I do not know if this question should be asked here or in stackoverflow since I already have a working code. Anyway I have asked the same question in stackoverflow also. Please feel free to correct me regarding the same.

Better way to query from multiple tables using Django ORM

I have the following models:

class Client(BaseModel):
    #other fields
    engineers = models.ManyToManyField(Engineer, null = True, blank = True, related_name='clients')

class OrganizationMember(MPTTModel):
    designation = models.CharField(max_length = 100, blank = True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', on_delete=models.SET_NULL)

Now I would like to get all engineers of a client instance and check if each engineer has an organizationmember instance or not.
Then if they belong to some organization then I should get_ancestors of that organizationmember and then the users.
If they do not have any organizationmember then just get the user of that engineer instance.

For this I have written a method in Client model as below:

class Client(BaseModel):

    def get_users_from_engineers(self):
        users_in_organization = ()
        users_outside_organization = ()

        engineers = self.engineers.all()
        if engineers:
            for engineer in engineers:
                user = engineer.user
                if hasattr(user, "organizationmember"):
                    users_in_organization += (orgmember.user for orgmember in user.organizationmember.get_ancestors(include_self=True))
                else:
                    users_outside_organization.append(user)

Now this code is working correctly but I am sure that I can use django filters here instead of two for loops and optimize this code. But I do not know how.

For eg:

OrganizationMember.objects.filter(user__engineer__in=self.engineers.all())

can be used to get all the organizationmembers of the engineers of that particular client instance. But I need the users of those organizationmembers.
Also if I use get_ancestors on multiple engineers then more than one engineer might have the same ancestor thereby adding the same person more than once in the users_in_organization list. How can I rectify this if I use django filters?

Note: This is my first question in codereview. I do not know if this question should be asked here or in stackoverflow since I already have a working code. Anyway I have asked the same question in stackoverflow also. Please feel free to correct me regarding the same.

python – Having a form and some plots on the same page with django?

I’m trying to learn use Bokeh and django to visualize some data, and I’m on a windows 10 laptop doing this project using VS code and Anaconda in a virtual environment, if this is relevant.

My goal is that the user shall be able to input a number into a field, hit submit, and the bokeh graph shall update. This means, if I understand it correctly that both bokeh graph and django form must lie under the same view-function in my views.py-script.

I have successfully created the graph, and am hitting difficulties with the form. I think it stems from me not properly understanding the POST vs GET requests.

Following a tutorial(https://www.youtube.com/watch?v=3XOS_UpJirU) I tested out the form feature by creating a forms.py with a very basic input form:

from django import forms
class InputForm(forms.Form):
scalar=forms.IntegerField(label="A number: ")

Then I created a basic “formtest.html” file just to display the form.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    <form method="POST">
    {% csrf_token %}
    {{form}}
    <button type="submit">Submit</button>
    </form>
</body>
</html>

And in views.py I added:

def formtest(request):
    #This form was  built to test that a value can be submitted and passed to the backend
    
    if request.method=="POST": 
        form=InputForm(request.POST)
        if form.is_valid():
            scalar=form.cleaned_data('scalar')
            print("This is the scalar:" )
            print(scalar)

    return render(request,'formtest.html',{'form':form})

This worked splendidly.

In order to show the form and my pre-existing plots on the same page, I simply copied what I had in the formtest()-function down to my view-function for my plots (and added an else), like this:

def starter(request):
    #setting up form
    if request.method=="POST": 
        form=InputForm(request.POST)
        if form.is_valid():
            scalar=form.cleaned_data('scalar')
            print("This is the scalar:" )
            print(scalar)
    else:
        form=None
     
   
    #Creating the plots
    {some irrelevant code for the plots}

    script,div=components(plots)

    return render(request,'starter.html',{'script': script, 'div': div,'form':form})

And in the starter.html file I added the forms, just as it had been in the formtest.html:

<!DOCTYPE html>
<html lang="en">
<head>

    <title>Bokeht</title>
</head>
<body>
    <h1>Bokeh Starter Graph</h1>
    <form method="POST">
        {% csrf_token %}
        {{form}}
        <button type="submit">Submit</button>
    </form>
  
    {{script|safe}}
    {{div|safe}}
   <div></div><!--Here is where the plot will be shown, since we called it div in
    script,div=components(plot)-->
   
</body>
{some cdn links for bokeh}
</html> 

I understand the issue seem to be that a GET request is sent, so the code never goes into the “if request.method=”POST”-clause. But when this was a separate view, it went right in there without me asking.

My confusion lies mainly in – Who decides that only a GET Request is sent? When the form was a standalone page, I never specified it was a POST request other than setting the -tag’s method to post, which I STILL DO. Or am I approaching it the completely wrong way?

I am going bananas over here. So what am I missing?

html – ¿Por qué Django intenta abrir una URL que no le indico?

¡Hola Comunidad!

Estoy creando un pequeño blog con Django, en el cual tengo una sola aplicación. Ocurre que ya tengo definido gran parte del blog, esto es:

  • La vista de Inicio.
  • Vistas para las categorías de cada publicación.
  • Vista para cada uno de los posts
  • Entre otras

Ahora que he querido agregar la vista de “Acerca del Autor”, cuando se debería redireccionar a su respectiva plantilla HTML, Django termina por redireccionarse hacía otra plantilla, el cual me genera un error de tipo NoReverseMatch

Simplificando el código, esto es:

views.py:

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post, Autor, Categoria

class Inicio(ListView):
    def get(self, request, *args, **kwargs):
        context = {
            'post': Post.objects.get(titulo='NombreDelPost')
        }
        return render(request, 'PaginaWebApp/home.html', context)

class PostSimple(DetailView):
    def get(self, request, slug_post, *args, **kwargs)
        
        context = {
            'post': Post.objects.filter(slug_post=slug_post)
        }

        return render(request, 'PaginaWebApp/page-simple.html', context)

class PostsCategoria(DetailView):
    def get(self, request, categoria, *args, **kwargs):
            # Vista que muestra cada una de las categorías dentro del blog
            context = {
                'categorias': Categoria.objects.get(categoria=categoria)
            }

            return render(request, 'PaginaWebApp/posts-category.html', context)

class AcercaAutor(DetailView):
    def get(self, request, slug_autor, *args, **kwargs):

        context = {
            'autor': Autor.objects.get(slug_autor=slug_autor)
        } 

        return render(request, 'PaginaWebApp/page-author.html', context)

urls.py:

from django.contrib import admin
from django.urls import path
from PaginaWebApp import views

urlpatterns = (
    path('', views.Inicio.as_view(), name='home'),
    # (Aquí van las URL hacia las otras plantillas del proyecto (ellas funcionan bien))
    # A continuación las conflictivas:
    path('posts-<categoria>/', views.PostsCategoria.as_view(), name='posts-category'),
    path('<slug:slug_post>/', views.PostSimple.as_view(), name='page-simple'),
    path('acerca-de-<slug:slug_autor>/', views.AcercaAutor.as_view(), name='page-author'),
)

Tengo una plantilla llamada “base.html” que heredan todas las demás.

En la plantilla de Inicio, llamada “home.html” podemos conseguir, entre otras cosas, lo siguiente:

<!-- AQUI VAN OTRAS ETIQUETAS QUE REDIRECCIONAN A OTRAS URL  -->
<h4><a href="{% url 'page-category' categorias.categoria %}" title="">Ver los posts de {{categorias.categoria}}</a></h4>
<h4><a href="{% url 'page-simple' post.slug_post %}" title="">{{post.titulo}}</a></h4>
<h4><a href="{% url 'page-author' autor.slug_autor %}" title="">Por: {{autor.nombre}}</a></h4>

Como mencioné anteriormente, al ingresar a la ventana de inicio “home.html” poseo una serie de etiquetas “a” que redireccionan a diversas plantillas, pero específicamente cuando selecciono a que vaya a la URL de la plantilla page-author.html, por alguna razón Django interpreta que debe redireccionarse a page-category y me genera el error descrito.

Reverse for 'posts-category' with arguments '('',)' not found. 1 pattern(s) tried: ('posts\-(?P<categoria>(^/)+)/$')

He revisado exhaustivamente cada plantilla HTML y todas están correctamente redireccionadas a las URL correspondientes.

Gracias de antemano por sus respuestas y comentarios.

Having problem to activate my Django environment in command line

First I created a new visual environment and then created my first project after that I left my PC. How I want to continue with the project I was working on and I don’t know how you go about it.

io – Resizing Image before converting to Base64 in Django

I’m working on a django application which has a “My Profile” section where users can upload there profile images. I’m storing the images as a base64 code and rendering on the site.
This is the code:

@login_required
def my_profile_edit(request):    
    if request.method == 'POST':
        form =ProfileEditForm(request.POST or None,request.FILES,instance=request.user)
        if form.is_valid():
            user=form.save(commit =False)
            user.user_image =form.cleaned_data('user_image')
            user.user_image =base64.b64encode(user.user_image.file.read())
            user.user_image ='data:image/jpeg;base64,'+ str(user.user_image)(2:-1)
            user.save()
            return redirect('my_profile')
    profile =CustomUser.objects.get(username=request.user)
    form =ProfileEditForm(instance=profile)
    return render(request,'profile_edit.html',{'form':form,'profile':profile})

Though it is working fine, I wish to reduce the size/ resize uploaded image first before encoding it into Base64 to improve performance and reduce space. I’ve tried few things using StringIO/BytesIO but without success. Please let me know the right way of doing so.

python – service apache2 start causes error in django

On running

apache2 configtest

I get

Syntax error on line 2 of
/etc/apache2/sites-enabled/firstweb.conf: Invalid command
‘WSGIScriptAlias’, perhaps misspelled or defined by a module not
included in the server configuration Action ‘configtest’ failed. The
Apache error log may have more information.

While
On running

apache2 -T

I get

[Sat Nov 07 16:04:57.447763 2020] [core:warn] [pid 23334] AH00111:
Config variable ${APACHE_RUN_DIR} is not defined apache2: Syntax error
on line 80 of /etc/apache2/apache2.conf: DefaultRuntimeDir must be a
valid directory, absolute or relative to ServerRoot

I can’t figure out the problem since I have installed mod_wsgi yet it is showing these errors. Any Idea?
I’m running on Ubuntu 20.04