I want to append td to my table using ajax but I have a little problem, how to add the source.twitter_id in url?? the error that I got is "Could not parse the remainder: ',' from ''twitter:source_delete',' "
I tried also {% url 'twitter:source_delete', source.twitter_id %} it also error.
javascript
function appendToUsrTable(source) {
console.log(source.twitter_id)
$("#sourceTable > tbody:last-child").append(`
<tr id="source-${source.twitter_id}">
<td>{{forloop.counter}}</td>
<td>${source.source_acct}</td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><a href="{% url
'twitter:source_delete' ${source.twitter_id} %}"
data-title="Delete" data-toggle="modal" data-target="#delete"><span ></span></a></p></td>
</tr>
`);
}
views.py
class CreateCrudUser(View):
def get(self, request):
name_source = request.GET.get('source_acct', None)
print(name_source)
obj = Source.objects.create(
source_acct = name_source
)
source = {'twitter_id':obj.twitter_id,'source_acct':obj.source_acct}
data = {
'source': source
}
return JsonResponse(data)
CodePudding user response:
this problem is raising because empty(null value) of source.twitter_id
so it is better to check if source.twitter_id is null or not
CodePudding user response:
You can try to define the url variable outside of java script template literal like this :
function appendToUsrTable(source) {
const url = "{% url 'twitter:source_delete' source.twitter_id %}"
console.log(source.twitter_id)
$("#sourceTable > tbody:last-child").append(`
<tr id="source-${source.twitter_id}">
<td>{{forloop.counter}}</td>
<td>${source.source_acct}</td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><a href=${url} data-title="Delete" data-toggle="modal" data-target="#delete"><span ></span></a></p></td>
</tr>
`);
NB : be careful about combine Django template variable with Java Script template literal.