gpt4 book ai didi

python - Django 让用户通过表单更新网站上的 TextField 值?

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:54 24 4
gpt4 key购买 nike

我希望有人能给我一些关于如何使用 Django 执行以下操作的帮助(如果我没有正确解释所有内容,请原谅,我对 Django 仍然很陌生,并且不了解很多事情):

我有一个电影表,这些电影有一个“描述”数据字段,当他们单击它时,会打开一个表单,其中包含电影的当前描述。如果他们双击此描述,则可以更改它,然后保存该值。我制作了一个小 gif 来形象化这个想法:

enter image description here

至少这就是这背后的基本想法,到目前为止,我已经成功地运行了大部分内容,但遗憾的是 Django 部分没有将来自用户的"new"数据发送到数据库并替换旧的描述的数据。

有人可以向我解释一下如何才能做到这一点吗?我知道我可能必须向 view.py 编写一个函数,然后创建一个新的 url 模式,但我只是不知道到底是怎么做的。欢迎任何帮助!下面是我的代码(我希望我已经包含了你们需要的每个文件):

views.py

 from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from django.views.generic.list import ListView
from .models import *

class AllMovies(generic.ListView):
model = Movie
template_name = "consilium/index.html"
context_object_name = "latest_movie_list"

class MovieDetails(generic.DetailView):
model = Movie
template_name = "consilium/detail.html"

urls.py

 from django.conf.urls import url
from . import views
from .models import *
from django.views.generic.list import ListView

app_name = "consilium"
urlpatterns = [
url(r'^$', views.AllMovies.as_view(), name="index"),
url(r'^(?P<slug>[\w_0-9]+)/$', views.MovieDetails.as_view(), name='detail'),
]

models.py

 from django.db import models
from decimal import Decimal
from django import forms
from django.contrib import admin

class Movie(models.Model):
// removed the other models for better overview
description = models.TextField('Movie Description')

def __str__(self):
return self.title

index.html

 {% extends "consilium/base.html" %}

{% block body %}
<table class="table">
<thead>
<tr>
<th></th>
<th colspan="2">My Movielist</th>
<th>
</tr>
<tr>
<th></th>
<th>TITLE</th>
<th>GENRE</th>
<th>RELEASE DATE</th>
<th>DIRECTOR</th>
<th>DESCRIPTION</th>
<th>RUNTIME</th>
<th>STATUS</th>
<th>IMDB</th>
</tr>
</thead>
<tbody>
{% if latest_movie_list %}
{% for movie in latest_movie_list %}
<tr>
<td></td>
<td>
<a href="{% url 'consilium:detail' movie.slug %}" data-toggle="popover" data-placement="left" data-content='<img class="title-image" src="{{movie.image.url}}"/>'>{{ movie.title }}</a>
</td>
<td>{{ movie.get_genre_display}}</td>
<td>{{ movie.date}}</td>
<td>{{ movie.director}}</td>
<td id="icn-change" data-toggle="collapse" data-target=".demo{{ forloop.counter }}">
Description <i class="fa fa-caret-right"></i>
</td>
<td>{{ movie.runtime}} min</td>
<td>{{ movie.get_status_display}}</td>
<td>{{ movie.imdb}}</td>
</tr>
<tr>
<td></td>
<td class="hiddenRow" colspan="8">
<div class="container collapse demo{{ forloop.counter }}">
<div class="row justify-content-center">
<div class="col">
<form method="post" id="usrform">{% csrf_token %}
<textarea id="text" class ="form-control" readonly="true" onkeydown="expandtext(this)" ondblclick="this.readOnly='';">{{movie.description}}</textarea>
</form>
</div>
</div>
<div class="row justify-content-center">
<div class="col align-self-start">Double Click to Edit</div>
<div class="col align-self-end">
<input type="submit" id="set" class="pull-right"/>
</div>
</div>
</div>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td>No Movies are available.</td>
</tr>
{% endif %}
</tbody>
</table>
{% endblock %}

script.js

 // removed all other code for overview

// replace description text with user input
$('#set').click(function() {
var test = $('#text').val();
localStorage.setItem("test", test);
});

$('#text').text(localStorage.getItem("test"));

我希望我没有错过任何事情,感谢所有可以帮助我的人!

最佳答案

我从事过一个类似的项目,这就是我所做的。

from django.forms.models import model_to_dict

@login_required
def edit_profile(request):
profile, created = ClientProfile.objects.get_or_create(user_id=request.user.id)
if request.method == 'POST':
form = ProfileSubmissionForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('jobs:list'))
else:
profile_dict = model_to_dict(profile)
form = ProfileSubmissionForm(profile_dict)
return render(request, 'jobs/profile.html', {'form': form})

本质上,model_to_dict 以表单形式呈现数据库中存储的值。 instance=profile 确保我正在更新表单而不是创建新对象。

关于python - Django 让用户通过表单更新网站上的 TextField 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com