在views.py
class ProcessMessage(TemplateView):
template_name = 'chat/chat.html'
def get_object(self):
id=self.kwargs.get('pk')
obj=None
if id is not None:
obj= User.objects.get(pk=id)
return obj
def get(self, request, pk=None,*args, **kwargs):
obj=self.get_object()
print(obj,pk)
super(ProcessMessage, self).get(request, *args, **kwargs)
return render(request, self.template_name, {'form': ChatForm()})
def post(self, request, pk=None,*args, **kwargs):
obj=self.get_object()
pk=pk
print(pk)
print('obj is',obj)
form = ChatForm(data=request.POST)
# log = logging.basicConfig(level=logging.DEBUG)
# print('post from index')
if form.is_valid():
//////something////
在urls.py中
app_name = 'chatbot'
urlpatterns = [
path('demo', views.ProcessMessage.as_view(), name='index'),
path('<uuid:pk>/demo', views.ProcessMessage.as_view(), name='index'),
]
我正在get
方法中获取obj和pk的值,但我也想要post
方法中的那些值(当前获取值是None)
我需要def Post方法中URL的id / pk来获取用户信息(不,我不想使用request.user)
模板:
{% extends 'chat/base.html' %}
{% load static %}
{% block head %}
<script type="text/javascript" src="{% static 'chat/js/somejavascript.js' %}"></script>
<style>
body {
overflow-x: hidden !important;
-ms-overflow-x: hidden !important;
}
/* width */
::-webkit-scrollbar {
width: 0;
background-color: transparent;
}
.btn-rounded {
border-radius: 30px;
font-size: 12px;
border-color: #3741e5;
}
.msg-btn{
font-size: 10px !important;
margin-bottom: 0;
padding-top: 3px!important;
padding-bottom: 3px!important;
}
</style>
{% endblock %}
{% block body %}
<div class="container mt-3 mx-1 msg-container">
{% if ad %}
<div class="ads d-flex " id="ads">
<div id="x" class="fa fa-times-circle red-text"></div>
<img src="{{ ad.image.url }}" class="blink-image ads">
</div>
{% endif %}
<!-- msg log-->
<div class="msg_log mb-5 pb-5" id="msg_log" style="overflow-x: hidden;overflow-y: auto;">
<div class="row incoming_msg_row d-flex justify-content-start mt-2 animated bounceInLeft">
<div class="col-10">
<div class="incoming_msg_box float-left">
<img src="{% static 'chat/img/logo.png' %}"
class="float-left img-circle chat-logo">
<div class="card z-depth-1 in-card">
<div class="card-body incoming_msg_body p-2">
<div class="incoming_msg msg-text">Hi, I am Virtual Assistant. How
can I help
you?<br>
Type 'cancel' if you get stuck anywhere
</div>
</div>
</div>
</div>
</div>
</div>
<!-- ./row-->
<div class="row btn-row">
<div class="col-12 btn-col">
<button type="button"
class="btn btn-outline-blue font-weight-bold msg-btn btn-sm waves-effect waves-light z-depth-1 px-2 btn-rounded animated zoomInUp">
Book Test Drive
</button>
<button type="button"
class="btn btn-outline-blue font-weight-bold msg-btn btn-sm waves-effect waves-light z-depth-1 px-2 btn-rounded animated zoomInUp">
Book Car Service
</button>
<button type="button"
class="btn btn-outline-blue font-weight-bold msg-btn btn-sm waves-effect waves-light z-depth-1 px-2 btn-rounded animated zoomInUp">
File Complaint
</button>
<button type="button"
class="btn btn-outline-blue font-weight-bold msg-btn btn-sm waves-effect waves-light z-depth-1 px-2 btn-rounded animated zoomInUp">
Feedback
</button>
<button type="button"
class="btn btn-outline-blue font-weight-bold msg-btn btn-sm waves-effect waves-light z-depth-1 px-2 btn-rounded animated zoomInUp">
Branch List
</button>
<button type="button"
class="btn btn-outline-blue font-weight-bold msg-btn btn-sm waves-effect waves-light z-depth-1 px-2 btn-rounded animated zoomInUp">
Get Price List
</button>
<button type="button"
class="btn btn-outline-blue font-weight-bold msg-btn btn-sm waves-effect waves-light z-depth-1 px-2 btn-rounded animated zoomInUp">
Request Call Back
</button>
<button type="button"
class="btn btn-outline-blue font-weight-bold msg-btn btn-sm waves-effect waves-light z-depth-1 px-2 btn-rounded animated zoomInUp">
FAQs
</button>
</div>
</div>
<div class="row mt-2 outgoing_msg_row first_out_msg animated bounceInUp "> <!-- -->
<div class="col-12">
<div class="outgoing_msg_box float-right">
<div class="card out-card z-depth-1">
<div class="card-body outgoing_msg_body p-2">
<div class="outgoing_msg msg-text">this is outgoing message</div>
</div>
</div>
</div>
</div>
</div>
{% if chats %}
{% for chat in chats %}
<div class="row mt-2 outgoing_msg_row" style="visibility: visible;">
<div class="col-12">
<div class="outgoing_msg_box float-right">
<div class="card blue-grey lighten-1 out-card">
<div class="card-body outgoing_msg_body p-2">
<div class="card-text outgoing_msg white-text msg-text">
{{ chat.message }}
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row incoming_msg_row mt-2">
<div class="col-12 ">
<div class="incoming_msg_box float-left">
<img src="https://branditprintit.com/wp-content/uploads/2017/04/logo-degin-logo-design-design-art-free.png"
class="float-left img-circle" style="height: 30px;width: 30px">
<div class="card z-depth-1 blue-gradient in-card">
<div class="card-body incoming_msg_body p-2">
<div class="incoming_msg white-text msg-text">
{% ifequal chat.response 'report_status_form' %}
REPORT STATUS FORM
{% else %}
{{ chat.response }}
{% endifequal %}
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
{% endif %}
<!-- ./row-->
</div>
<!-- ./msg log-->
<!-- form row-->
<div class="row chatbox-container fixed-bottom pb-0 mt-2 py-0">
<div class="col-12">
<img src="{% static 'chat/eztyping.gif' %}" id="preload"
style="visibility: hidden;display:none; height: 50px; width: 50px;">
<form method="post" id="chat-form">
{% csrf_token %}
<div class="shadow-textarea d-flex justify-content-center">
<div class="message-box-div">
{{ form.message }}
<button type="submit" class="send-btn">
<i class="fa fa-paper-plane"></i>
</button>
</div>
</div>
</form>
</div>
</div>
<!-- ./form row-->
</div>
{% endblock %}
<!-- end of container -->
{% block footerjs %}
<script>
$('#message').keydown(function (e) {
if (e.keyCode == 13) {
e.preventDefault();
if ($.trim($('#message').val()) != "") {
send_message($('#message'), " {% url 'chat:index' %}");
}
}
});
$('#chat-form').on('submit', function (event) {
event.preventDefault();
console.log("form submitted!"); // sanity check
send_message($('#message'), " {% url 'chat:index' %}");
});
</script>
<script>
$(document).ready(function () {
$(document).on('click', '.msg-btn', function () {
//$('#message').val('cancel');
//send_hidden_message($('#message'), " {% url 'chat:index' %}",$(this).text());
$('.btn-row').hide();
msg = $(this).text();
$('#message').val(msg);
send_message($('#message'), " {% url 'chat:index' %}");
});
});
$(document).ready(function () {
$(document).on('click', '.send-loc-btn', function () {
if ($(this).text().indexOf('yes') != -1 || $(this).text().indexOf('Yes') != -1) {
sendlocation();
} else {
incoming_msg_row = $('.incoming_msg_row').first().clone();
incoming_msg_row.css('visibility', 'visible');
incoming_msg_row.find('.incoming_msg_box').html('Operation cancelled');
$('.msg_log').append(incoming_msg_row);
}
});
});
</script>
<script>
if (_iOSDevice = !!navigator.platform.match(/iPhone|iPod/)) {
$('#msg_log').css('height', '85vh');
$('#message').css('height', '10vh');
// modal.style.height = `calc(100vh - ${browserUiBarsH}px)`;
}
</script>
<script>
function send_link_message(ref) {
msg = $(ref).text();
// $('#message').val('cancel');
// send_hidden_message($('#message'), " {% url 'chat:index' %}");
$('#message').val(msg);
send_message($('#message'), " {% url 'chat:index' %}");
}
$(document).ready(function () {
$('#x').click(function () {
$('#ads').css('height', '0px');
$('#ads').css('visibility', 'hidden');
});
});
</script>
<div class="animated fadeOutDown"></div>
{% comment %}
<script>
if (window.performance) {
console.info("window.performance works fine on this browser");
alert('works fine');
}
if (performance.navigation.type == 1) {
console.info("This page is reloaded");
alert('reloaded');
} else {
console.info("This page is not reloaded");
alert('not reloaded');
}
</script>
{% endcomment %}
{% endblock %}
您的模板显示您正在将表单提交到不带pk的URL:
{% url 'chat:index' %}
例如,在反转URL时,您应该包括pk
{% url 'chat:index' pk %}
例如,您需要在模板上下文中包含pk
return render(request, self.template_name, {'form': ChatForm(), 'pk': pk})
[我看到您对ProcessMessage
和/demo/
使用了相同的视图/<pk>/demo/
。我建议对两个URL使用不同的视图,我认为这将有助于避免出现此类问题。