Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Not understanding field__icontains?

I'm having a problem implementing a database query using a string.
I've tried many different methods with all of the same results, and am nowhere closer to the goal.

#urls.py
url(r'^api/v1/item/nomen/contains/(?P<nomen>[\w\-]+)/$', views.ItemListContainsNomen.as_view(), name='item_lookup_contains_nomen'),


#views.py
class ItemListContainsNomen(generics.ListAPIView):
serializer_class = ItemSerializer

def get_queryset(self):
nomen = self.kwargs['nomen']
return Item.objects.filter(nomenclature__icontains=nomen)

class Item(models.Model):
id = models.IntegerField(primary_key=True)
nomenclature = models.CharField(max_length=35)

This nets me a warning such as
> "/.virtualenvs/realDjango/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py:110: Warning: Truncated incorrect DOUBLE value: 'nomen/contains/CLOSER'
>
> return self.cursor.execute(query, args)
>
> Not Found: /api/v1/item/nomen/contains/CLOSER/"

I can successfully run this query using the MYSQL console and raw MYSQL with LIKE, and I can successfully query another database's TextField using the same syntax in Django.....but this column just doesn't seem to work no matter what. There has to be something I'm missing. I should also note, replacing icontains=nomen with say, icontains="CLOSER" also yields the same errors.
Checking datatype in console shows

> +-----------+
> | DATA_TYPE |
> +-----------+
> | varchar |
> +-----------+
> 1 row in set (1.39 sec)

/r/django
https://redd.it/6a0tz9
[HELP] My details page isn't shown, why?

Hi, people. I'm a beginner Django developer so sorry if it's a basic question.

I have a webpage that shows a list of movies and each movie has a details view, but for some reason, the details view is never rendered.

#views.py

def index(request):
latest_movies = Movie.objects.order_by('-movie_id')[:5]

template = loader.get_template('ytsmirror/index.html')
context = {
'latest_movies' : latest_movies,
}

return HttpResponse(template.render(context, request))

def detail(request, movie_id):

movie = Movie.objects.get(movie_id=movie_id)
template = loader.get_template('ytsmirror/details.html')

context = {
'movie' : movie,
'plot': 'Lorem impsum',
}

return HttpResponse(template.render(context, request))

And my urls.py

#urls.py

from django.conf.urls import url

from . import views

app_name = 'ytsmirror'
urlpatterns = [
url(r'$', views.index, name='index'),
url(r'^(?P<movie_id>\d{4})$', views.detail, name='detail'),
]

When I try to reach /ytsmirror/4200/ for example, I don't get any error and Django apparently reaches the correct URL pattern but doesn't render the details view, it stays on the index view, without any change.

What am I doing wrong?

/r/djangolearning
https://redd.it/6lhjxy
i want to Redirect to the same view after delete??

**so after deleting comment i can't back to detailviews of my comments , i need back with the pk of the post**

\#error

`Reverse for 'CommentsBack' not found. 'CommentsBack' is not a valid view function or pattern`



#views
# list all the comments of ever post l
class comment_back (DetailView):
model=Post
template_name='CommentsBack.html'

#delete comment by his primary key
def delete_comment(self,pk):
event=Comment.objects.get(pk=pk)
event.delete()  
return redirect('CommentBack')

&#x200B;

#urls

path('mypost/<int:pk>/commentBack', comment_back.as_view(),name="commentBack"),
path('DeleteComment/<int:pk>/remove', views.delete_comment,name="delete_comment"),

/r/djangolearning
https://redd.it/uplkto