Solution 1 :

It seems like you haven’t created User class yet. You need related_name to reference the Register model through User

models.py:

from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
from django.contrib.auth.models import User

# Create your models here.
class Register(models.Model):
    username = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
    name = models.CharField(max_length=50)
    idCard = models.ImageField(upload_to='idCard', null=True)

    def __str__(self):
        return self.name

forms.py:

from django import forms
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import *

class UserForm(ModelForm):
    class Meta():
        model = User
        fields = ('username')

class RegisterForm(ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Your full name...'}))

    class Meta:
        model = Register
        fields = '__all__'

preview.html:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Preview</title>
</head>
<body>

Hey {{prev.name}},
your idCard photo is <img src="{{prev.profile.idCard.url}}" alt="idcard">

</body>
</html>

Problem :

I am building a form where a user can upload an image. After clicking on the submit button he/she will be redirected to a preview page to see all submitted image. But the image is not showing up in the review page whereas the image is visible from the admin site. After clicking on the inspect, it shows the image source is unknown. Here’s my code:

models.py

from django.db import models
from phonenumber_field.modelfields import PhoneNumberField

# Create your models here.
class Register(models.Model):
    name = models.CharField(max_length=50)
    idCard = models.ImageField(upload_to='idCard', null=True)

    def __str__(self):
        return self.name

forms.py

from django import forms
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import *

class RegisterForm(ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Your full name...'}))

    class Meta:
        model = Register
        fields = '__all__'

views.py

from django.shortcuts import render, redirect
from .models import *
from .forms import *
# Create your views here.
def index(request):
    form = RegisterForm()
    if request.method == 'POST':
        form = RegisterForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save()
            return redirect('preview', pk=instance.id)
    context = {'form':form}
    return render(request, 'event/index.html', context)

def preview(request, pk):
    reg = Register.objects.get(id=pk)
    prev = RegisterForm(instance=reg)
    if request.method == 'POST':
        form = RegisterForm(request.POST, instance=reg)
        if form.is_valid():
            form.save()
            return redirect('/')
    context = {'reg':reg, 'prev':prev}
    return render(request, 'event/preview.html', context)

urls.py

from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
    path('', views.index, name='home'),
    path('preview/<str:pk>', views.preview, name="preview"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

preview.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Preview</title>
</head>
<body>

Hey {{prev.name}},
your idCard photo is <img src="{{prev.idCard.url}}" alt="idcard">

</body>
</html>

index.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Event Registration</title>
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    <script src="{% static 'js/script.js' %}"></script>
</head>
<body>
    <div class="mobile-screen">
        <div class="header">
        </div>

        <div class="logo"></div>

        <form id="login-form" method="POST" action="" enctype="multipart/form-data">
            {% csrf_token %}
                {{form.name}}
                <legend style="color: aliceblue;">Upload ID card: </legend>{{form.idCard}}
                <input class="btn btn-sm btn-primary" type="submit" value="Register" name="Register">
        </form>

      </div>
</body>
</html>

Help me load the image in the preview page

Comments

Comment posted by Mochida

Try changing your

Comment posted by Deepjyoti De

It didn’t work.

By