How to pass data from a button in the html page to a database?
I have to code a page that has a question, and 3 options to answer.
I have a page that display the questions and the 3 answers of every question, the questions are already stored in the database. Now my problem is how can I save the user choice for that specific question?
I have created a model to save the user choice, the user id, and the question id. But I'm having trouble to get witch button the user clicked for the answer.
#Here is the model
class UserVotes(models.Model):
id_user = models.CharField(max_length=10)
id_question = models.CharField(max_length=10)
choice_user = models.CharField(max_length=10)
#Here is the view
class QuestionListView(generic.ListView):
model = Question
def voteQuestion(request):
if 'Yes' in request.POST:
idQuestion = request.question.id
idUser = request.user.id
p = UserVotes(id_user=idUser, id_question=idQuestion, choice_user=1)
p.save()
elif 'No' in request.POST:
#same thing, the difference is the value of choice_user will be 2.
#the url.py
url(r'^question/$', views.QuestionListView.as_view(), name='question'),
#the html page
<button type="submit" name="Yes">Yes</button>
<button type="submit" name="No">No</button>
I've got several errors, the last one that came up is a http 405 error, method post not allowed.
If someone can help please, I've spend 2 nights and 1 day on this already haha. Thanks!!
/r/django
https://redd.it/8gae5m
I have to code a page that has a question, and 3 options to answer.
I have a page that display the questions and the 3 answers of every question, the questions are already stored in the database. Now my problem is how can I save the user choice for that specific question?
I have created a model to save the user choice, the user id, and the question id. But I'm having trouble to get witch button the user clicked for the answer.
#Here is the model
class UserVotes(models.Model):
id_user = models.CharField(max_length=10)
id_question = models.CharField(max_length=10)
choice_user = models.CharField(max_length=10)
#Here is the view
class QuestionListView(generic.ListView):
model = Question
def voteQuestion(request):
if 'Yes' in request.POST:
idQuestion = request.question.id
idUser = request.user.id
p = UserVotes(id_user=idUser, id_question=idQuestion, choice_user=1)
p.save()
elif 'No' in request.POST:
#same thing, the difference is the value of choice_user will be 2.
#the url.py
url(r'^question/$', views.QuestionListView.as_view(), name='question'),
#the html page
<button type="submit" name="Yes">Yes</button>
<button type="submit" name="No">No</button>
I've got several errors, the last one that came up is a http 405 error, method post not allowed.
If someone can help please, I've spend 2 nights and 1 day on this already haha. Thanks!!
/r/django
https://redd.it/8gae5m
reddit
r/django - How to pass data from a button in the html page to a database?
1 votes and 4 so far on reddit
How to Create An Twitterbot with simpletwitter
Github Repository: [https://github.com/pravee42/simpletwitter](https://github.com/pravee42/simpletwitter)
```
from simpletwitter import SimpleTwitter
email = "Twitter_User_Email_Address"
password = "Twitter_Password"
user_name = "Abipravi1"
#here i have entered my twitter username but you need to enter your's in this case
no_of_tweets = 10 #this value is necessary how many no of tweets you want to perform operation
bot = SimpleTwitter(email, password, no_of_tweets, user_name)
#Creating Instance
hashtags = ['#abipravi', #pythonmodule', '#twitter_bot']
tweetmessage = "My first tweet by simple twitter"
bot.login() # to login into the account
bot.like_tweet(hashtags) # like the tweet
bot.unlike_liked_tweets(5) # unlike the liked tweet
bot.tweet(tweetmessage) # put some tweet
/r/Python
https://redd.it/r7mtzy
Github Repository: [https://github.com/pravee42/simpletwitter](https://github.com/pravee42/simpletwitter)
```
from simpletwitter import SimpleTwitter
email = "Twitter_User_Email_Address"
password = "Twitter_Password"
user_name = "Abipravi1"
#here i have entered my twitter username but you need to enter your's in this case
no_of_tweets = 10 #this value is necessary how many no of tweets you want to perform operation
bot = SimpleTwitter(email, password, no_of_tweets, user_name)
#Creating Instance
hashtags = ['#abipravi', #pythonmodule', '#twitter_bot']
tweetmessage = "My first tweet by simple twitter"
bot.login() # to login into the account
bot.like_tweet(hashtags) # like the tweet
bot.unlike_liked_tweets(5) # unlike the liked tweet
bot.tweet(tweetmessage) # put some tweet
/r/Python
https://redd.it/r7mtzy
GitHub
GitHub - pravee42/simpletwitter: Twitter bot module with few lines of code
Twitter bot module with few lines of code. Contribute to pravee42/simpletwitter development by creating an account on GitHub.
Help with UpdateView and getting key:value from ModelForm
views.py
def WantListUpdateView(request, pk, username=None):
context = {}
def getusername(self):
username = self.request.user.username
context['data'] = {'username' : username}
initialdata = CoinWantlist.objects.get(pk=pk) #This works
initialdata = initialdata.meta.getfields() #HERE IS WHERE I NEED HELP, how do I get the key : value passed into context.
return render (request, 'wantlist/wantlist.html', context)
I'm trying to make an update view that prefills the form data into the form for the user to update. The URL works but I need help getting the KEY : VALUE passed into context to create the initial data.
I'm sure this is a simple .something.get.something that I just haven't been able to find yet.
/r/django
https://redd.it/rurzz9
views.py
def WantListUpdateView(request, pk, username=None):
context = {}
def getusername(self):
username = self.request.user.username
context['data'] = {'username' : username}
initialdata = CoinWantlist.objects.get(pk=pk) #This works
initialdata = initialdata.meta.getfields() #HERE IS WHERE I NEED HELP, how do I get the key : value passed into context.
return render (request, 'wantlist/wantlist.html', context)
I'm trying to make an update view that prefills the form data into the form for the user to update. The URL works but I need help getting the KEY : VALUE passed into context to create the initial data.
I'm sure this is a simple .something.get.something that I just haven't been able to find yet.
/r/django
https://redd.it/rurzz9
reddit
Help with UpdateView and getting key:value from ModelForm
views.py def WantListUpdateView(request, pk, username=None): context = {} def get_username(self): username =...
Django-filter; creating a variable which is callable from the view
I have a filter which can be used to filter the products on brand(s), which looks like this:
​
As you can see, the brand filter returns a flat list of all possible brands in the categorie 'eiwitten'. Is there a way to make the queryset in brand a variable which I can input from my view function? This way I can use the filter for every page. Resulting in a unique brand list depending on the categorie. My view function for the 'eiwitten' page looks like this:
/r/django
https://redd.it/10houtj
I have a filter which can be used to filter the products on brand(s), which looks like this:
class SortFilter(django_filters.FilterSet): ORDER_BY_CHOICES = ( ('-discount_sort', 'Hoogste korting'), ('-new_price', 'Hoogste prijs'), ('new_price', 'Laagste prijs'), ) order_by = django_filters.ChoiceFilter(label='Sorteer op', choices=ORDER_BY_CHOICES, method='filter_by_order', empty_label=None)#HERE I WOULD LIKE THIS INPUT A VARIABLE INTO THE QUERYSET variable of the filter brand = django_filters.ModelMultipleChoiceFilter(queryset=Product.objects .order_by('brand') .filter(categorie='eiwitten') .values_list('brand', flat=True).distinct() , widget=forms.CheckboxSelectMultiple) q = django_filters.CharFilter(method='multiple_field_search', widget=TextInput(attrs= {'placeholder': 'Zoek..'})) class Meta: model = Product fields = ['brand', 'q'] def filter_by_order(self, queryset, name, value): return queryset.order_by(value) def multiple_field_search(self, queryset, name, value): return queryset.filter( Q(title__icontains=value) | Q(categorie__icontains=value) | Q(brand__icontains=value) )​
As you can see, the brand filter returns a flat list of all possible brands in the categorie 'eiwitten'. Is there a way to make the queryset in brand a variable which I can input from my view function? This way I can use the filter for every page. Resulting in a unique brand list depending on the categorie. My view function for the 'eiwitten' page looks like this:
def eiwit(request): #print(request.GET.getlist('brand')) # filter alleen eiwitproducten /r/django
https://redd.it/10houtj
reddit
Django-filter; creating a variable which is callable from the view
I have a filter which can be used to filter the products on brand(s), which looks like this: `class SortFilter(django_filters.FilterSet):` ...
is there a way to make pyright recognize related name fields?
from django.db import models
class A(models.Model):
id = models.IntegerField()
class B(models.Model):
id = models.IntegerField()
fkA = models.ForeignKey(A, ondelete=models.CASCADE, relatedname="fkB")
a = A(id=1)
b = B(id=2, fkA=a)
a.fkB #Here it says it cannot access attribute fkB for class A
take for example the code snippet above. is there a way to make pyright know that fk\B fields for class A exists?
/r/django
https://redd.it/1fzlr75
from django.db import models
class A(models.Model):
id = models.IntegerField()
class B(models.Model):
id = models.IntegerField()
fkA = models.ForeignKey(A, ondelete=models.CASCADE, relatedname="fkB")
a = A(id=1)
b = B(id=2, fkA=a)
a.fkB #Here it says it cannot access attribute fkB for class A
take for example the code snippet above. is there a way to make pyright know that fk\B fields for class A exists?
/r/django
https://redd.it/1fzlr75
Reddit
From the django community on Reddit
Explore this post and more from the django community