Home > OS >  Django CRUD works but wrong path
Django CRUD works but wrong path

Time:10-26

@login_required(login_url='/users/login')
@permission_required('users.change_user', login_url='users/login') 
def edit(request, profile_id):
    try:
        user = User.objects.get(pk=profile_id)
    except User.DoesNotExist:
        raise Http404("Profile does not exist")
    return render(request, 'UI/edit.html', {'users': user})


def processedit(request, profile_id):
    user = get_object_or_404(User, pk=profile_id)
    profile_pic = request.FILES.get('image')
    try:
        fname = request.POST.get('fname')
        lname = request.POST.get('lname')
        email = request.POST.get('email')
        position = request.POST.get('position')
    except (KeyError, User.DoesNotExist):
        return render(request, 'UI/detail.html', {'user': user, 'error_message': "Problem updating record"})
    else:
        user_profile = User.objects.get(id=profile_id)
        user_profile.user_fname = fname
        user_profile.user_lname = lname
        user_profile.user_email = email
        user_profile.user_position = position
        if profile_pic:
            user_profile.user_image = profile_pic
        user_profile.save()
        return HttpResponseRedirect(reverse('users:detail', args=(profile_id,)))

from views.py

here from edit.html

{% extends 'base.html' %}

{% block title %}
    {{ user.user_fname }} {{ user.user_lname }}
{% endblock %}

{% block content %}

{% if error_message %}
    <p >
        <strong>{{error_message}}</strong>
    </p>
{% endif %}
        <h1> Edit User Profile</h1>
    <form action="{% url 'users:processedit' user.id %}" method="post" enctype="multipart/form-data">
        {% csrf_token %} 

        <div >
            <label>First Name</label>
            <input type="text" name="fname" id="fname"  required value="{{ users.user_fname }}">
        </div>

        <div >
            <label>Last Name</label>
            <input type="text" name="lname" id="lname"  required value="{{ users.user_lname }}">
        </div>

        <div >
            <label>Email</label>
            <input type="text" name="email" id="email"  required value="{{ users.user_email }}">
        </div>

        <div >
            <label>Position</label>
            <input type="text" name="position" id="position"  required value="{{ users.user_position }}">
        </div>

        <div ><br>    
            <label>User Image</label><br><br>
            <input type="file" name="image" id="image">
        </div>
        <br>
        <button type="submit" >Update</button>
        
    </form>

{% endblock %}

when I try to Edit, it edits the first user from database, not the selected user, there's no error appears, it works but wrong route/path

How do I fix this? It was working right before.

I did created two user, both with super and staff status, is that fine too?

I am currently searching / debugging again

Created new user, details are unique, now when I Update it, it says this

IntegrityError at /users/1/processedit/

CodePudding user response:

you have make an error in context variable.

@login_required(login_url='/users/login')
@permission_required('users.change_user', login_url='users/login') 
def edit(request, profile_id):
    user = get_object_or_404(User, pk=profile_id)
    return render(request, 'UI/edit.html', {'user': user})

After change "users" to "user" in your rendering context, i think it vill be better.

And you have to uniformize variable in your template:

{% extends 'base.html' %}

{% block title %}
    {{ user.user_fname }} {{ user.user_lname }}
{% endblock %}

{% block content %}

{% if error_message %}
    <p >
        <strong>{{error_message}}</strong>
    </p>
{% endif %}
        <h1> Edit User Profile</h1>
    <form action="{% url 'users:processedit' user.id %}" method="post" enctype="multipart/form-data">
        {% csrf_token %} 

        <div >
            <label>First Name</label>
            <input type="text" name="fname" id="fname"  required value="{{ user.user_fname }}">
        </div>

        <div >
            <label>Last Name</label>
            <input type="text" name="lname" id="lname"  required value="{{ user.user_lname }}">
        </div>

        <div >
            <label>Email</label>
            <input type="text" name="email" id="email"  required value="{{ user.user_email }}">
        </div>

        <div >
            <label>Position</label>
            <input type="text" name="position" id="position"  required value="{{ user.user_position }}">
        </div>

        <div ><br>    
            <label>User Image</label><br><br>
            <input type="file" name="image" id="image">
        </div>
        <br>
        <button type="submit" >Update</button>
        
    </form>

{% endblock %}
  • Related