Hi Nitin,
Thank you for your comment.
I did
jango-admin startproject mysite
and cd mysite, then
python manage.py runserver
and server is working, so I did
opython manage.py startapp polls
polls app was successfully created, and stopped the server because I wanted
to user postgresql instead of the default SQLite3(?).
I did below
pg_ctl -D /user/local/var/posgres start
postgres successfully started running, then stopped
*By the way, I registered polls app in settings.py (I just copied and
pasted part of codes from the file below)*
INSTALLED_APPS = [
'polls.apps.PollsConfig',
*Also, I followed the way to add postgres as database, I looked as Django
docs*
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Koitaro',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
*I did migration too, like migrate, makemigration etc, just copied and
pasted from Django Docs*
*This is my models.py*
import datetime
from django.db import models
from django.utils import timezone
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
*mygration was successfully generated*
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Choice',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('choice_text', models.CharField(max_length=200)),
('votes', models.IntegerField(default=0)),
],
),
migrations.CreateModel(
name='Question',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('question_text', models.CharField(max_length=200)),
('pub_date', models.DateTimeField(verbose_name='date
published')),
],
),
migrations.AddField(
model_name='choice',
name='question',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
to='polls.Question'),
),
]
*When I go to admin, I added 5 or 6 questions, and successfully added in
database.*
*the database name I made is 'Koitaro'.*
*I registered in settings.py and I have this database named as Koitaro in
my posgresql*
question has data, but choice thing doesn't seem having any data...
I really appreciate your thoughts.
Nori
On Sunday, February 3, 2019 at 1:56:18 AM UTC-5, Nitin Kalmaste wrote:
>
> Have you migrated database and added any question there?
>
> On Sun, Feb 3, 2019, 8:39 AM Atsunori Kaneshige <[email protected]
> <javascript:> wrote:
>
>> Oh, one note is that I am using postgresql.
>> everything else except vote function in views.py seems working.
>>
>> Sorry, any help would be really appreciated!
>>
>> Nori
>>
>> On Saturday, February 2, 2019 at 10:02:14 PM UTC-5, Atsunori Kaneshige
>> wrote:
>>>
>>> Hi Django users,
>>>
>>> I started using Django recently.
>>> I am following the official Django tutorial.
>>> I am just at Writing Your First Django App, Part4, and I have been just
>>> copying and pasting all codes.
>>>
>>> But I have a problem in vote.
>>> I inserted print(question) and this is printed in detail.html
>>> also, question.id is also printed.
>>>
>>> BUT, choice part doesn't seem working.
>>>
>>> *<THIS IS views.py>*
>>> def vote(request, question_id):
>>> question = get_object_or_404(Question, pk=question_id)
>>> print(question)
>>> try:
>>> selected_choice =
>>> question.choice_set.get(pk=request.POST['choice'])
>>> except (KeyError, Choice.DoesNotExist) as e:
>>> # Redisplay the question voting form.
>>> print(e)
>>> return render(request, 'polls/detail.html', {
>>> 'question': question,
>>> 'error_message': "You didn't select a choice.",
>>> })
>>> else:
>>> selected_choice.votes += 1
>>> selected_choice.save()
>>> # Always return an HttpResponseRedirect after successfully
>>> dealing
>>> # with POST data. This prevents data from being posted twice if a
>>> # user hits the Back button.
>>> return HttpResponseRedirect(reverse('polls:results', args=(
>>> question.id,)))
>>>
>>> *<THIS IS details.html>*
>>> <h1>{{ question.question_text }}</h1>
>>>
>>> {% if error_message %}<p><strong>{{ error_message }}</strong></p>{%
>>> endif %}
>>>
>>> <form action="{% url 'polls:vote' question.id %}" method="post">
>>> {% csrf_token %}
>>> {% for choice in question.choice_set.all %}
>>> <input type="radio" name="choice" id="choice{{ forloop.counter }}"
>>> value="{{ choice.id }}">
>>> <label for="choice{{ forloop.counter }}">{{ choice.choice_text
>>> }}</label><br>
>>> {% endfor %}
>>> <input type="submit" value="Vote">
>>> </form>
>>>
>>> <br>
>>> {{ question }}
>>> #printed
>>> <br>
>>> {{ question.id }}
>>> #printed
>>> <br>
>>> *{{ question.choice_set.all }}*
>>> *#<QuerySet []> #what is this? empty? why?*
>>> <br>
>>> {{ question.question_text }}
>>> #printed
>>> <br>
>>> <h1>{{ question.question_text }}</h1>
>>> #printed
>>> <ul>
>>> *{% for choice in question.choice_set.all %}*
>>> * <li>{{ choice.choice_text }}</li>*
>>> *{% endfor %}*
>>> *#nothing printed!*
>>> </ul>
>>>
>>> Also when I click button 'vote', I only get error.
>>> *You didn't select a choice.*
>>>
>>> I am just copying and pasting so that I can understand Django, but I am
>>> having hard time in this Part 4.
>>>
>>> I really appreciate advice from anybody!
>>>
>>> Nori
>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/a581da0f-abd9-435e-8693-db9126b9bac1%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/django-users/a581da0f-abd9-435e-8693-db9126b9bac1%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/b1699379-b28e-4900-a06c-4267efbb4446%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.