[英]How do I manipulate text submitted in a Django form?
I want the user to be able to input a word, click "submit", and then see their word in all caps on the next page.我希望用户能够输入一个单词,单击“提交”,然后在下一页上看到他们的单词全部大写。 (This is not my final intention, just a useful way to progress).
(这不是我的最终意图,只是一种有用的进步方式)。
views.py:视图.py:
from django.http import HttpResponse
from django.template import loader
from .models import Word
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import WordForm
def wordinput(request):
if request.method == 'POST':
form = WordForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = WordForm()
return render(request, 'word.html', {'form': form})
def your_word(request):
form = WordForm()
if request.method == 'POST': # and form.is_valid():
word = WordForm(request.POST)
word = str(word)
word = word.upper()
return HttpResponse(word)
else:
return HttpResponse("that didn't work")
forms.py: forms.py:
from django import forms
class WordForm(forms.Form):
your_word = forms.CharField(label='Type your word here', max_length=100, required = True)
word.html:字.html:
<form action="/wordsearch/your-word/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Search">
</form>
urls.py:网址.py:
from django.contrib import admin
from django.urls import include, path
from . import views
urlpatterns = [
path('wordinput/', views.wordinput, name='wordinput'),
path('your-word/', views.your_word, name='your_word')
]
Result: TYPE YOUR WORD HERE: ASDF结果:在此处输入您的单词:ASDF
((In this result, "ASDF" is in a box and manipulable)) ((在这个结果中,“ASDF”在一个盒子里并且可以操作))
Desired result: ASDF期望的结果:ASDF
((The desired result is simply on the screen)) ((想要的结果只是在屏幕上))
Instead of word = str(word)
, use word = str(word.cleaned_data["your_word"])
.代替
word = str(word)
,使用word = str(word.cleaned_data["your_word"])
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.