How can I fix my code For Django's reply comment - python

When entering a reply comment,
It will be redirected to my post with comments
How can I fix it? Please Someone help me
post_detail.html
<!-- Reply Button Area -->
<button class="btn btn-sm btn-secondary" type="button" style="float:right" data-toggle="collapse" data-target="#replyBox{{comment.id}}" aria-expanded="false" aria-controls="replyBox{{comment.id}}">
Reply
</button>
<div class="collapse" id="replyBox{{comment.id}}">
<div class="card card-body my-2">
<form action="/blog/{{ comment.pk }}/reply_comment/" method="post">
{% csrf_token %}
<div class="form-group">
<label for="comment">Reply </label>
<input type="text" class="form-control" name="comment" placeholder="type the comment">
<input type="hidden" name="parentSno" value="{{comment.id}}">
</div>
<input type="hidden" name="post.pk" value="{{post.pk}}">
<button type="submit" class="btn btn-sm btn-primary">Complete</button>
</form>
</div>
</div>
<!-- Reply Button Area END -->
Views.py
def reply_comment(request , comment_pk) :
if request.user.is_authenticated :
comment = get_object_or_404(Comment , pk=comment_pk)
if request.method == 'POST' :
recomment_form = ReCommentForm(request.POST)
if recomment_form.is_valid() :
recomment = recomment_form.save(commit=False)
recomment.author = request.user
recomment.comment = comment
recomment.save()
return redirect(comment.get_absolute_url())
return redirect(comment.post.get_absolute_url())
else :
raise PermissionError
It's the github address with the code
Thank you !!!
https://github.com/JaehyoJJAng/doitdjnago_help/tree/c36a75bf089ceb027c9cf9f5c184ac37f77f3788

Related

Modal window not called after button click

I'm making my first website, using Django, python and bootstrap 5, I want a person to click the "submit" button after filling out the form and after that a modal window pops up in which it would say that "everything is OK, your application has been created"
<div class="row mt-5 mb-5 p-2">
<form action="{% url 'feedback_view' %}" method="POST">
{% csrf_token %}
<div class="col-md-6 mb-2">
<label for="exampleInputName" class="form-label">Ваше Имя</label>
<input name="name" class="form-control" id="exampleInputName" aria-describedby="NameHelp">
</div>
<div class="col-md-6 mb-2">
<label for="exampleInputphone" class="form-label">Ваш Телефон</label>
<input name="phone" class="form-control" id="exampleInputphone" aria-describedby="NameHelp" placeholder="+7 (918) 000-00-00">
</div>
<button type="submit" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">send</button>
</form>
</div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">send_btn</button>
but for some reason the "send" button does not work, but the "send_btn" button works, which I made to check the problem. all I understand at the moment is what bothers me, type="submit" because in the afternoon there is type="button". How can I solve this problem, given that this is a form submission button? this is the window code:
{% if messages %}
{% for message in messages %}
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p class="reviews">{{ message }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
{% endfor %}
{% endif %}
it's views.py
class FeedBackView(View):
def post(self, request):
form = FeedBackForm(request.POST)
if form.is_valid():
form.save()
messages.add_message(request, settings.MY_INFO, 'now call you!')
else:
messages.add_message(request, settings.MY_INFO, 'something was wrong')
return redirect("/")

2 form at the same page flask

Hello guys hope u doing fine, I got this project and in home page of my website i have two form
1 to login and the other to upload a file however every time i submit with any form the same function keeps getting executed please help.
this is the html code
<!--login-->
<section class="bg-dark text-light p-5 text-center">
<div class="container">
<h2>First step </h2>
<form action = "http://localhost:5000" method="post" name="login" >
<div class="col">
<div class="hide-md-lg">
<h4>Sign in to your <span class="text-warning">Linkedin account</span> </h4>
</div>
<br>
<input type="text" name="username" placeholder="Username" required>
<br><br>
<input type="password" name="password" placeholder="Password" required>
<br><br>
<input class="btn btn-primary" type="submit" value="Login">
</div>
</form>
</div>
</section>
<!--upload excel-->
<section class="bg-dark text-light p-4">
<div class="container bg-primary rounded border-0 p-5">
<div class="d-md-flex justify-content-between align-items-center">
<div class="container">
<h5 class="mt-2">Choose an excel file</h5>
<h6 class="mb-3"><span class="text-warning">NB: first 2 columns have to be: "Prénom" and "Nom"</span></h6>
</div>
<form class="input-group" action = "http://localhost:5000" method = "POST" enctype = "multipart/form-data" name="upload">
<input class="form-control size-file" type="file" name="file" id="formFile">
<input class="btn btn-dark" type = "submit" value="Start scraping" >
</form>
</div>
</div>
</section>
and this is the backend part
#app.route('/', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST' :
f = request.files['file']
f.save(secure_filename(f.filename))
df = pd.read_excel(f.filename)...............
#app.route('/', methods = ['GET', 'POST'])
def login_info():
if request.method == 'POST' and 'Sign in to your' in request.form['h4']:
user_info = request.form.get("username")
pass_info = request.form.get("password")
Although unable to try it this way, I updated your code with different action parameters.
<!--login-->
<section class="bg-dark text-light p-5 text-center">
<div class="container">
<h2>First step </h2>
<form action = "{{url_for('login_info')}}" method="post" name="login" >
<div class="col">
<div class="hide-md-lg">
<h4>Sign in to your <span class="text-warning">Linkedin account</span> </h4>
</div>
<br>
<input type="text" name="username" placeholder="Username" required>
<br><br>
<input type="password" name="password" placeholder="Password" required>
<br><br>
<input class="btn btn-primary" type="submit" value="Login">
</div>
</form>
</div>
</section>
<!--upload excel-->
<section class="bg-dark text-light p-4">
<div class="container bg-primary rounded border-0 p-5">
<div class="d-md-flex justify-content-between align-items-center">
<div class="container">
<h5 class="mt-2">Choose an excel file</h5>
<h6 class="mb-3"><span class="text-warning">NB: first 2 columns have to be: "Prénom" and "Nom"</span></h6>
</div>
<form class="input-group" action = "{{url_for('upload_file')}}" method = "POST" enctype = "multipart/form-data" name="upload">
<input class="form-control size-file" type="file" name="file" id="formFile">
<input class="btn btn-dark" type = "submit" value="Start scraping" >
</form>
</div>
</div>
</section>
And the #app.route should lead to two function :
#app.route('/upload_file', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST' :
f = request.files['file']
f.save(secure_filename(f.filename))
df = pd.read_excel(f.filename)...............
#app.route('/login_info', methods = ['GET', 'POST'])
def login_info():
if request.method == 'POST' and 'Sign in to your' in request.form['h4']:
user_info = request.form.get("username")
pass_info = request.form.get("password")
If you want both those page to return to your home screen, you just have to tell them to return to this same template with render_template() or redirect()

pass value for two different submit buttons in same form methods in Django

Lets us consider my template.html as
<form class="form-horizontal" id="adhoc-form" method="post" action="{% url 'contacts:add_item' item.id %}">
{% csrf_token %}
<fieldset>
<div class="control-group">
<label for="id_item_filename" class="control-label">Items
<span class="text-error">*</span></label>
<div class="controls">
<select placeholder="Item filename" name="item" id="id_item_filename" class="span3" required="required">
<option value="">---------</option>
{% for i in items %}
<option value="{{i.0}}">{{i.1}}</option>
{% endfor %}
</select>
</div>
</div>
</fieldset>
<div id="form-buttons-container" class="form-actions" style="padding-left: 0px;">
<div class="controls">
<input type="hidden" class="btn btn-primary btn-medium" id= 'i_id' name='i_id' value="{{data.0.id}}">
<input type="submit" class="btn btn-primary btn-medium" value="Submit">
<input type="submit" class="btn btn-primary btn-medium" value="Delete">
</div>
</div>
</form>
my url.py is
url(r'^stock/item/add/item_name/(?P<id>\d+)/$', login_required(UpdateBarcode.as_view()), name="add_item"),
my views.py is
class UpdateItem(View):
def post(self, request, id):
item_id = request.POST.get('item')
items = Items.objects.get(id=item_id)
try:
JobItems.objects.filter(id=id).update(item_name=items.name)
except:
messages.error(request, 'Cannot update')
return redirect(reverse("contacts:item_list"))
Here when clicking the submit button we need to update item and we click delete button item should be deleted.please help me how can do both the submit and delete operations in the same view
You can get started with updating your HTML to give buttons name field:
<input type="submit" class="btn btn-primary btn-medium" name="update-item" value="Submit">
<input type="submit" class="btn btn-primary btn-medium" name="delete-item" value="Delete">
Now you can check which button is triggered. (update-item or delete-item):
def post(self, request, id):
item_id = request.POST.get("item")
items = Items.objects.get(id=item_id)
if "update-item" in request.POST:
try:
JobItems.objects.filter(id=id).update(item_name=items.name)
except:
messages.error(request, "Cannot update")
elif "delete-item" in request.POST:
JobItems.objects.get(id=id).delete()
return redirect(reverse("contacts:item_list"))
Note that I don't know your logic for item-delete operation so you can update here.

How to send data to view from form after asking for confirmation?

I created a simple form where I'm asking for user input and then posting that to the database. Now I would like the user to confirm the data in form is correct, and for that I'm using a Bootstrap modal.
How can I send the data from the form to the view when pressing the 'OK' button on the modal.
I'm new to the Django framework, and maybe there is another better way without using bootstrap modals.
Form:
class ReportForm(forms.Form):
report_title = forms.ChoiceField(choices=get_report_titles())
report_link = forms.CharField(widget=forms.Textarea, required=False)
Html file:
<form class="w-100" action="" method="post">
{% csrf_token %}
<label class="pl-0 mt-auto mr-2">Report Name</label>
<select name="report_title" class="form-control report-name">
<option selected>Select report</option>
{% for name in report_names %}
<option>{{ name }}</option>
{% endfor %}
</select>
<div class="my-1 col-lg-12 float-left pl-0">
<label>Report Link</label>
<input class="form-control bg-white" type="text" id="report" name="report_link">
</div>
<input id="confirm" value="Save" type="button" data-toggle="modal" data-target="#exampleModalCenter" class="btn btn-outline-success" />
</form>
<!-- Modal -->
<div class=" modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Confirmation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Make sure you have the right title and link.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
View:
def report_view(request):
if request.method == 'POST':
form = ReportForm(request.POST)
if form.is_valid():
report_title = form.cleaned_data['report_title']
report_link = form.cleaned_data['report_link']
new_report = Report(title = report_title, link = report_link)
new_report.save()
Simply update your code with these lines, add id="exampleForm".
start form tag with
<form class="w-100" action="" id="exampleForm" method="post">
Replace Save button with (add id="save"):
<button type="button" id="save" class="btn btn-primary">Save</button>
and finally add this script at the bottom to submit your form when save is clicked:
<script>
$("#save").on("click", function(e) {
$("#exampleForm").submit();
});
</script>
also I feel your view is not written correctly. It should be something like this, I'm not sure what you're trying to do:
def report_view(request):
if request.method == 'POST' and form.is_valid():
form = ReportForm(request.POST)
report_title = form.cleaned_data['report_title']
report_link = form.cleaned_data['report_link']
new_report = Report(title = report_title, link = report_link)
new_report.save()
Let me know if you still need help
This needs changing because you're not giving any values.
<select name="report_title" class="form-control report-name">
<option selected>Select report</option>
{% for name in report_names %}
<option>{{ name }}</option>
{% endfor %}
</select>
Try:
<select name="report_title" class="form-control report-name">
<option selected>Select report</option>
{% for name in report_names %}
<option value="{{name.pk}}">{{ name }}</option>
{% endfor %}
</select>
where name.pk is the value relating to the choices.
Also, if you change your form to a ModelForm you can just do:
if form.is_valid():
new_report = form.save()

How to add a button in email input?

I am trying to put a button inside an email input in a Django form.
This is the code with the button next to the email input:
<div class="container" id="notifyEmailFormContainer">
<form action="{% url "landing:notify_email_add" %}" method="post" id="notifyEmailForm">
{% csrf_token %}
<div class="form-group row">
<div class="col-sm-10 input-group" id="emailInput">
<div class="input-group-addon" id="comingSoonEmailIcon"><i class="fa fa-envelope fa fa-2x" aria-hidden="true"></i></div>
<input class="form-control" type="email" name="email" placeholder="Your email...." maxlength="255" required id="id_email"/>
</div>
<div class="col-sm-2">
<button type="button" class="btn btn-block btn-primary" onclick="addNotifyEmail()" id="submitNotifyEmail">Notify Me</button>
</div>
</div>
</form>
</div>
I have a tried a couple answers in similar questions but it seems I can't get it to work.

Categories