I've searched enough about this problem, but haven't been able to find a solution.
Please help me.
The board detail view and comment template containing the comment list were all imported.
I brought it in as much detail as possible, so I hope you give me a way.
View.py
class comment_delete(DeleteView):
model = Comment
success_url = reverse_lazy('board_list.html')
class board_detail(generic.DetailView):
def get(self, request, *args, **kwargs):
pk = self.kwargs['pk']
rsDetail = Board.objects.filter(b_no=pk)
rsData = Board.objects.get(b_no=pk)
rsData.b_count += 1
rsData.save()
comment_list = Comment.objects.filter(Board_id=pk).order_by('-id')
return render(request, "board_detail.html", {
'rsDetail': rsDetail,
'comment_list' : comment_list
})
def post(self, request, *args, **kwargs):
bno = get_object_or_404(Board, b_no=self.kwargs['pk'])
cnote = request.POST.get('c_note')
cwriter = request.POST.get('c_writer')
rows = Comment.objects.create(
Board = bno,
c_note = cnote,
c_writer = cwriter
)
return redirect(reverse('board_detail', kwargs={'pk': self.kwargs['pk']}))
urls.py
path('', views.home, name="home"),
path('board/', views.board.as_view(), name="board"),
path('board/<int:pk>/', views.board_detail.as_view(), name="board_detail"),
path('board_write/', views.board_write, name="board_write"),
path('board_insert', views.board_insert.as_view(), name="board_insert"),
path('board_edit/', views.board_edit, name="board_edit"),
path('board_update/', views.board_update, name="board_update"),
path('board_delete/', views.board_delete, name="board_delete"),
path('board/comment/update/', views.comment_update, name="comment_update"),
path('board/comment/<int:pk>/delete/', views.comment_delete.as_view(), name="comment_delete")
comment.html
{% if not comment_list %}
<p class="text-center"> empty </p>
{% endif %}
<div style="margin:20px 0; margin-right:400px; margin-left:400px;">
{% for i in comment_list %}
<table width="100%" cellpadding="0" cellspacing="0">
<tbody>
<tr style="background-color:#f1eee3;height:45px;border-top: 1px solid #333;">
<td style="padding-left: 10px;border-top: 1px solid #eee;width: 114px;" align="center" width="20%">{{ i.c_writer }}</td>
<td style="vertical-align: top;border-top: 1px solid #eee;padding: 10px 0;" align="left" width="70%">{{ i.c_note }}
<span style="color: #999;font-size: 11px;display: block;">{{ i.c_date }}</span>
</td>
{% if user.username == i.b_writer or user.is_staff %}
<form action="{% url 'comment_update' %}">
<td style="vertical-align: top;border-top: 1px solid #eee;padding: 10px 0;" align="right" width="10%">
<button class="btn btn-outline-success my-2 my-sm-0">edit</button>
</td>
</form>
<form action="{% url 'comment_delete' pk=i.Board_id %}" method='POST'>
{% comment %} <form action="{% url 'comment_delete' Board_id=i.Board_id id=i.id %}" method='POST'> {% endcomment %}
{% csrf_token %}
<td style="vertical-align: top;border-top: 1px solid #eee;padding: 10px 0;padding-right: 5px;" align="right" width="10%">
<button class="btn btn-outline-success my-2 my-sm-0" style="margin-right:10px;">delete</button>
</td>
</form>
{% endif %}
</tr>
</tbody>
</table>
{% endfor %}
{% include 'comment_write.html' %}
Unless specified otherwise the delete view except only the pk from the object you want to delete so in your case that would be the comment :
path('board/comment/<int:pk>/delete/', views.comment_delete.as_view(), name="comment_delete")
Also you were not using the comment ID but the board ID, which of course is different and will cause your url to point the incorrect object.
The same logic applies to the DetailView, UpdateView, DeleteView, those views expects the primary key (or ID) that you reference in the model attribute within those views.
Last thing, what is i.Board_id and id.id ? When using variable be the most explicit, do name your variables in lower case as well.
Related
{% extends "IntakeApp/base3.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block heading %}
<h2>Allergies for {{request.session.report_claimant}}</h2>
{% endblock %}
{% block content %}
<form hx-post="{% url 'allergy' %}" hx-target="#allergy_target" hx-swap="outerHTML">{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-2 mb-0">
{{ form.allergen|as_crispy_field }}
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
<div class="container-fluid">
<table class="table table-striped table-sm" id="med-table">
<thead>
<tr>
<th>Allergen</th>
</tr>
</thead>
<tbody id="allergy_target">
{% for allergy in allergy_list %}
<tr>
<td>{{allergy.allergen}}</td>
<td>
<form method="POST" action="{% url 'allergy-delete' allergy.id %}">{% csrf_token %}
<input class="btn btn-danger btn-sm" type="submit" value="Delete">
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
class AllergyCreateView(generic.CreateView):
model = Allergy
template_name = 'IntakeApp/allergy_form.html'
form_class = AllergyForm
def form_valid(self, form):
form.instance.assessment = Assessment.objects.get(id=self.request.session['assessment_id'])
return super(AllergyCreateView, self).form_valid(form)
def get_success_url(self):
return reverse_lazy("allergy")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
assessment_id = self.request.session['assessment_id']
allergy_list = Allergy.objects.filter(assessment=assessment_id)
context["allergy_list"] = allergy_list
return context
I tried to all the different hx-swap options, but none fix it...It does post correctly and the swap does work, just not sure why I am getting another form in there. Please help
I added the View above. I think thats were my issue is...not sure if I am supposed to be doing this way or not?
Seems like you're trying to render the same html content in your view, which instead should only take a specific element.
You can try to use hx-select="#allergy_target" so htmx will only fetch the table content.
i am trying to save data in a table when a user click the checkout button of add to cart but it is showing me the above mention error i am unable to understand and one more thing is happening when i logout my the cart which i saved also got erased is it shomehow related to that i don't know
here is my views.py for checkout button
class Checkout(View):
def post (self, request,):
user = request.session.get('user')
ids = (list(request.session.get('cart').keys()))
sections = Section.get_sections_by_id(ids)
for section in sections:
order = Order(user = User(id=user),
section = section,
price = section.price,
)
order.save()
my views.py for cart.html
class Cart(View):
def get (self, request):
ids = (list(request.session.get('cart').keys()))
sections = Section.get_sections_by_id(ids)
print(sections)
return render(request, 'cart.html', {'sections': sections})
my urls.py
urlpatterns = [
path('cart/', Cart.as_view(),name='cart'),
path('Check-Out/', Checkout.as_view(),name='checkout'),
]
my cart.html
{% extends 'base.html' %}
{% load static %}
{% load cart %}
{% load custom %}
{% block head %}
<link rel="stylesheet" href="{% static 'css/cart.css' %}">
{% endblock %}
{% block content %}
<div class="container jumbotron">
<section>
<h1>My cart</h1>
<table class="table">
<thead>
<tr>
<th scope="col">S.no</th>
<th scope="col">Subject</th>
<th scope="col">Section</th>
<th scope="col">Teacher</th>
<th scope="col">Duration</th>
<th scope="col">Price</th>
</tr>
</thead>
{% for section in sections%}
<tbody style="margin-bottom: 20px;">
<tr>
<th scope="row">{{forloop.counter}}</th>
<td>{{section.subject.name}}</td>
<td>{{section.title}}</td>
<td>{{section.teacher}}</td>
<td>{{section.content_duration}}</td>
<td>{{section.price|currency}}</td>
</tr>
</tbody>
{% endfor %}
<tfoot>
<tr>
<th> Total</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th>{{sections|total_price:request.session.cart|currency}}</th>
</tr>
<hr>
</tfoot>
</table>
<button type="button" data-toggle="modal" data-target="#exampleModal" style="float: right; margin-left:5px" class="btn btn-outline-primary">Check Out</button>
<button type="button" style="float: right; margin-left:5px" class="btn btn-info">Back To Site</button>
</tfoot>
</section>
</div>
<!-- Modal -->
<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">Please Verify</h5>
<input type="button"class="btn-close btn-link" style="text-decoration:none; border:none; font-size:20px;" data-dismiss="modal" aria-label="Close" value="X">
</div>
<div class="modal-body">
<form action="{% url 'transactions:checkout' %}" method="Post">
{% csrf_token %}
<input type="submit" class="btn float-right btn-primary" value='Go Ahead'>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
and my models.py
class Order(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,)
section = models.ForeignKey(Section, on_delete=models.CASCADE )
price = models.FloatField(blank=False)
update_at = models.DateTimeField(auto_now=True, editable=False)
def placeorder(self):
self.save()
please help if you can an
The issue caused by this line:
order = Order(user = User(id=user)
Using User(id=user) means you want to create an unsaved User and use it in an unsaved Order and then saving the order, but this will not work because you haven't saved the User yet, as mentioned by the error.
You can just simply just use the existing user in the order like this:
order = Order(user=user, section=section, price=section.price)
order.save()
I'm fairly new at this. I'm trying to build a report page in iframe according to user requirement user can create report with src, width and height ...and i successfully done this...i am able to create the report now i want this created report name will be show in the dropdown menu and when the user click on report name then user can see the report and the name of the report will add on dynamically in the dropdown....
i'm waiting for response ..here i'm going to share the code what i have done ...
i would say one more thing i don't want to add these data (Src,width ,height,name of the report) in the data base ...is that possible to create report and get the same report when i will click on the report name.
index.html
<li class="dropdown">
Reports<span class="caret"></span>
<ul class="dropdown-menu">
<li class="dropdown-header">Reports</li>
<li>
<div class="buttons pull-right">
<i class="fa fa-plus"></i>
</div>
Report one
</li>
{% for item in report_item %}
<li>
{{item.name}}
</li>
{% endfor %}
</ul>
</li>
reportform.html
<form action = "add" method= "post" enctype="multipart/form-data" class="form form-horizontal">
{% csrf_token %}
<div class="panel panel-default">
<div class="panel-heading">
<strong>Add Report</strong>
</div>
<div class="panel-body">
<table class="table table-hover report-body attr-table">
<tr>
<td>URL</td>
<td>
<input type="text" name="src">
</td>
</tr>
<tr>
<td>WIDTH</td>
<td>
<input type="number" name="width">
</td>
</tr>
<tr>
<td> HEIGHT</td>
<td>
<input type="number" name="height">
</td>
</tr>
<tr>
<td> NAME OF THE REPORT</td>
<td>
<input type="text" name="name">
</td>
</tr>
</table>
<input type="submit">
</div>
</div>
</form>
report_one.html
<iframe src = {{src}} width= {{width}} height= {{height}} frameborder="0" allowfullscreen allowtransparency ></iframe>
**view.py **
def reportone(request):
return render(request, 'report_one.html')
def reporttwo(request):
return render(request, 'report_two.html')
def reporttest(request):
return render(request, 'reportform.html')
def add(request):
if request.method == "POST":
report_item={}
src=request.POST['src']
width=request.POST['width']
height=request.POST['height']
name=request.POST['name']
report_item={'src':src, 'width':width, 'height':height, 'name':name}
return render(request, 'report_one.html', report_item)
else:
return render(request, 'report_one.html' , report_item)
urls.py
from django.contrib import admin
from django.urls import path
from extras.views import ObjectChangeLogView
from . import views
app_name = 'report'
urlpatterns = [
path('reportone', views.reportone, name='reportone'),
path('reporttwo', views.reporttwo, name='reporttwo'),
path('reporttest', views.reporttest, name='reporttest'),
path('add', views.add, name='add'),
]
Please try this:
models.py
from django.db import models
class Report(models.Model):
url = models.URLField(null=False,max_length=300)
width = models.IntegerField(default=300)
height = models.IntegerField(default=200)
name = models.CharField(max_length = 50)
forms.py
from .models import Report
from django.db import models
class ReportForm(forms.Form):
width = forms.CharField(widget=forms.NumberInput(attrs={'class':' form-control'}))
height = forms.CharField(widget=forms.NumberInput(attrs={'class':' form-control'}))
url = forms.URLField(max_length=300)
name = forms.CharField(max_length = 50)
class Meta:
model = Report
fields = [ "url","width","height","name"]
views.py
from .forms import ReportForm
from .models import Report
def reporttest(request):
form = ReportForm()
return render(request, 'reportform.html',{'form':form})
def add(request):
report_item={}
if request.method == "POST":
obj= Report()
obj.url =request.POST['url']
obj.width=request.POST['width']
obj.height=request.POST['height']
obj.name=request.POST['name']
obj.save()
report_item={'src':request.POST['url'], 'width':request.POST['width'], 'height':request.POST['height'], 'name':request.POST['name']}
return render(request, 'report_one.html', report_item)
else:
return render(request, 'report_one.html' , report_item)
reportform.html
<form action = "add" method= "post" enctype="multipart/form-data" class="form form-horizontal">
<div class="panel panel-default">
<div class="panel-heading">
<strong>Add Report</strong>
</div>
<div class="panel-body">
<table class="table table-hover report-body attr-table">
{% csrf_token %}
{% for field in form.visible_fields %}
<tr>
<td>{{field.label}}</td>
<td>{{field}}</td>
</tr>
{% endfor %}
</table>
<input type="submit" >
</div>
</div>
</form>
report_one.html
<iframe width="{{width}}" height="{{height}}" src="{{src}}" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
please try this.After change model then migrate it.
simple url for checking :https://www.youtube.com/embed/F5mRW0jo-U4
I'm trying to do a query when the object has the status = 'Opened'. And display in a table where I will have a button to give a solution for my form and change status='Pending'. But when I click in the button I get this error.
What I'd like to do for really is display was a form for each data, but when I do a for loop for each one form my data insnt show, how you can see my editable.html. I just get the buttons to do an action, and they are working fine.
url:
path('manutencao_os_status/<int:id>', views.manutencao_ordem_de_servico, name='manutencao_os_status'),
path('manutencao_ordem_de_servico/', views.manutencao_ordem_de_servico, name='manutencao_ordem_de_servico'),
views.py
def manutencao_ordem_de_servico(request):
ordem_servico = OrdemServico.objects.filter(status='Aberto').order_by('id')
form = FormOrdemServico(ordem_servico)
if request.method != 'POST':
return render(request, 'ordemservico/manutencao_ordem_de_servico.html', {
'form': form,
'ordem_servico': ordem_servico
})
form = FormOrdemServico(request.POST, ordem_servico)
if not form.is_valid():
return render(request, 'ordemservico/manutencao_ordem_de_servico.html', {
'form': form,
'ordem_servico': ordem_servico
})
ordem_servico.save()
return redirect('ordemservico:manutencao_ordem_de_servico')
def manutencao_os_status(request, id):
ordem_servico = OrdemServico.objects.get(pk=id)
ordem_servico.status = 'Em Aprovação'
ordem_servico.save()
return redirect('ordemservico:manutencao_os_status')
html:
{%extends 'base.html' %}
{%block conteudo %}
<h1>Ordens de Serviço</h1>
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="card card-primary">
<div class="table table-bordered">
<table class="table table-bordered">
<thead>
<tr>
<td>Condition:</td>
<td>ID:</td>
<td>Name:</td>
<td>Status:</td>
<td>Solution:</td>
</tr>
</thead>
<tbody>
{% for os in ordem_servico %}
<tr>
<td>
<a href="{% url 'ordemservico:manutencao_os_status' os.id %}"
class="btn btn-success">Aprovar</a>
</td>
<td>{{os.id}}</td>
<td> {{os.name}}</td>
<td>{{os.status}}</td>
<td>{{os.solution}}</td>
</tr>
{%endfor%}
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
{% endblock %}
my editable.html:
{%for os in ordem_servico %}
<form action="{% url 'ordemservico:manutencao_os_status' os.id %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset>
<legend><h2>Ordens de Serviço</h2></legend>
<table class="table">
{{ os.form }}
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary">Solucionar</button>
</td>
</tr>
</table>
</fieldset>
</form>
{%endfor%}
{% endblock %}
You routed to the wrong view, it should be:
path(
'manutencao_os_status/<int:id>',
views.manutencao_os_status,
name='manutencao_os_status'
),
In the view you should likely redirect to the manutencao_ordem_de_servico view:
def manutencao_os_status(request, id):
ordem_servico = OrdemServico.objects.filter(pk=id).update(
status='Em Aprovação'
)
return redirect('ordemservico:manutencao_ordem_de_servico')
Note: A GET request is not supposed to have side-effects, hence updating
objects when a user makes a GET request, is not compliant with the HTTP
standard. Therefore it might be better to update a OrdemServico with a POST request.
My delete view is not working and the error message is:
Page not found (404) Request Method: GET.
I am trying to delete my uploaded file based on its primary key and so far, my url is able to link me correctly based on the pk.
This is my urls.py:
path('post/<int:post_id>/lesson_delete/<int:lesson_id>', LessonDeleteView.as_view(), name='lesson_delete'),
My views.py:
class LessonDeleteView(DeleteView):
model = Lesson
success_url = '../'
template_name = 'lesson_confirm_delete.html'
This is the html template that brings user to the delete view:
{% extends "store/base.html" %}
{% block content %}
<div id="main">
<table class="table mb-0">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Download</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for l in Lesson %}
<tr>
<td>
{% if l.file %}
{{ l.title }}
{% else %}
<h6>Not available</h6>
{% endif %}
</td>
<td>{{ l.post.author }}</td>
<td>{% if l.file %}
Download
{% else %}
<h6>Not available</h6>
{% endif %}
</td>
<td> <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'lesson_delete' lesson_id=l.id %}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
This is my html template for DeleteView:
{% extends "store/base.html" %}
{% block content %}
<div class="content-section" id="main">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Delete Lesson</legend>
<h2>Are you sure you want to delete the post "{{object.title}}"?
</h2>
</fieldset>
<span style="display:inline;">
<button class="btn btn-outline-danger" type="submit">Yes, Delete!
</button>
<a class="btn btn-outline-secondary" href ="">Cancel</a>
</span>
</form>
</div>
{% endblock content %}
This is my Lesson Model:
class Lesson(models.Model):
title = models.CharField(max_length=100)
file = models.FileField(upload_to="lesson/pdf")
date_posted = models.DateTimeField(default=timezone.now)
post = models.ForeignKey(Post, on_delete=models.CASCADE, null=False, blank=False)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('lesson_upload', kwargs={'pk': self.pk})
Is you template called 'lesson_confirm_delete.html'?
Also, for your success url, I have feeling you don't have a path '../'. It should the specific path you want it to go to.
(Sorry, I can't comment yet.)