0

I've got my LAMP setup with Ubuntu 20.04.4, Apache, MariaDB, and of course Python. The Django code managed to create some tables for me in the database but I just get 404 when I try to load my form in my browser via URL http://127.0.0.1/vcm/create_unit. But it works with the Python dev server at http://127.0.0.1:8000/vcm/create_unit.

Edit: Solved... sudo systemctl restart apache

It seems Apache doesn't rerun the python files so modifications don't take effect. Restarting Apache seems to do the job. The Python dev server doesn't suffer from this problem. So I guess while developing the Python dev server is a good option.

The code below seems to be working.

Apache apache2.conf

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

WSGIScriptAlias / /opt/vcm_project/vcm_project/wsgi.py
WSGIPythonHome /opt/env
WSGIPythonPath /opt/vcm_project

<Directory /opt/vcm_project/vcm_project>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

From TEMPLATES in settings.py I've hardcoded the path to my .html

'DIRS': [ '/opt/vcm_project/vcm/templates/vcm' ],

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('create_unit', views.create_unit, name='create_unit'),
]

models.py

from django.db import models

# Create your models here.
class Unit(models.Model):
    unit_code = models.CharField(max_length=15)
    unit_name = models.CharField(max_length=255)
    unit_sector = models.CharField(max_length=255)
    unit_application = models.TextField(max_length=65535)
    unit_url = models.URLField(max_length=255)
    superseded_equivalent = models.CharField(max_length=15)

    def __str__(self):
        return self.unit_name

class Element(models.Model):
    element_number = models.CharField(max_length=100)
    element_name = models.CharField(max_length=255)

    def __str__(self):
        return self.element_name

forms.py

from django.forms import ModelForm
from .models import Unit

class UnitForm(ModelForm):
    class Meta:
        model = Unit
        fields = ('__all__')

views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

from .models import Unit
from django.views.generic.edit import CreateView, UpdateView

def index(request):
    return HttpResponse("Hello, world. You're at the vcm index.")


def create_unit(request):
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = UnitForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            form.save()
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = UnitForm()

    return render(request, 'vcm/create_unit.html', {'form': form})

My HTML is here /opt/vcm_project/vcm/templates/vcm/create_unit.html

create_unit.html

<form action="{% url 'create_unit' %}" method="POST">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit"/>
</form>

admin.py

from django.contrib import admin

# Register your models here.
from .models import Unit

admin.site.register(Unit)


from .models import Element

admin.site.register(Element)
Harley
  • 1,305
  • 1
  • 13
  • 28
  • Please elaborate which url you're trying to access. – Ivan Starostin Jun 07 '22 at 07:45
  • `http://127.0.0.1/create_unit` or `http://127.0.0.1/vcm/create_unit` – Harley Jun 07 '22 at 07:46
  • 1
    Since LAMP mentioned I guess you're trying to access page via Apache, so it can be a mistake in Apache config. Please do the `runserver` and try to access Django app directly via `:8000` port (by default). Currently it is unclear which part of setup is broken. – Ivan Starostin Jun 07 '22 at 07:57
  • `http://127.0.0.1/admin/` works. So does `http://127.0.0.1/vcm/` The LAMP setup is good. – Harley Jun 07 '22 at 07:59
  • You're on to something though... I'm getting different results from each server – Harley Jun 07 '22 at 08:02

0 Answers0