У меня простой Django вид, который отображает текст, который пользователь вводит при вводе. Теперь этот текст отображается на этой же странице без изменения адреса (scraping. html), но я хотел бы отобразить его на новой странице (scrapingscore. html). Как я могу это сделать? Я прочитал о HttpResponseRedirect и попытался сделать это, но это не сработало, и я в замешательстве.
views.py
class HomeView(TemplateView):
template_name = 'scraping.html'
def get(self, request):
form = HomeForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = HomeForm(request.POST)
if form.is_valid():
text = form.cleaned_data.get('post')
form = HomeForm()
args = {'form': form, 'text': text}
return render(request, self.template_name, args)
forms.py
class HomeForm(forms.Form):
search = forms.CharField(label='search', max_length=100)
Соскоб. html
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload text</button>
</form>
{{ text }}
{% endblock %}
edit:
теперь мой код выглядит так:
urls.py
path('scraping', views.HomeView.as_view(), name='scraping'),
path('scrapingscore', views.HomeView.as_view(), name='scrapingscore'),
views.py
class HomeView(TemplateView):
template_name = 'scraping.html'
def get(self, request):
form = HomeForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = HomeForm(request.POST)
if form.is_valid():
text = form.cleaned_data.get('post')
form = HomeForm()
c = {text: 'bar'}
return render(request, 'scrapingscore.html', context = c)
и я добавил '{{text}}' к скрепингскору. html