Starting polls again

I am starting the polls project for the second time. I decided this time to go through the beginning instructions religiously. I work my way down to “python manage.py run server” which should work fine, but I get "No module named “able2.settings”. Of course, right there it is in the file structure — able2/able2/settings.py.

able2 is the name I gave the project in PyCharm. So, the project actually looks like this: Able2/able2/able2/settings.py

what gives?

Best guess is that you’re in the wrong directory when trying to run this.

For “able2.settings” to be a valid reference, you’d need to be in Able2/able2 as your current directory.

What directory is your manage.py in?

(Also, and I’m guessing it’s a typing error when composting your message, but it’s python manage.py runserver - runserver as 1 word, not “run server” as 2 words.)

Ken

Yes on the typo, but Able2/able2/able2

There I have init.py. asgi.py. settings.py urls.py. wsgi.py. which probably all should be up a level.

I think I have an extra able2 folder somehow

Now I get an error that Django is not installed, which of course it is. I am throwing everything away and starting over. I did this just fine the first time.

No, those are the right directory for those files.

Which means your current directory for running the manage.py command should be one level up, Able2/able2

Python considers directories to be modules. (A bit of a simplification, but close enough for discussion purposes for now.)

So if your current directory is Able2/able2, then a reference to the module able2/settings would be a file in Able2/able2/able2/settings.py.

Having the extra directory isn’t a problem as long as your current directory is the right directory for your file structure.

Ken

Wow. So, I put everything at the right level and Django disappeared.
Starting over once again. I could not find a way to resolve the issue
of “no polls.detail” when it is right there in plain sight. So, I am
going to try to work all the way down to it again and see if it is still
a problem. It simply doesn’t make any sense.

Plus, I got through the “NoReverse…” stuff and how have " TemplateDoesNotExist for a template name “polls/question_detail.html” which definitely DOES NOT exist. I have looked through my code to find where the program came up with this address and can’t find. It is the result of clicking on one of the questions that appear in the questions list. It has to be in the code somewhere doesn’t it. I am going to try a search in PyCharm on “question_detail”

That would be an indication of something going wrong in the latter steps of part 4.
See Amend views and the text below the example where it explains about question_detail being the default template being looked for if you don’t have the generic view defined correctly.

OK. Well, if followed you lead and after awhile I found the problem.
It was great except now there is another problem. Oh, that’s how it
works, right?

Thank you Ken.

Ralph

This is driving me crazy. There is something fundamentally wrong with what I am doing. I am going back into projects in PyCharm and trying to run them. I can’t even get to the index page with getting a 404 telling me its tried the URL patterns, say polls/ or admin/ and the empty path doesn’t match any these. This is projects that were working. Now I am going back into them looking for hints and I get this all the time. I always check the pwd and it is correct. I just don’t understand what has happened.

Sounds more like a Python environmental issue rather than a Django configuration issue. I’d look at how your packages are installed and see how things work directly from the command line rather than through PyCharm to see what might be going wrong that may be masked by PyCharm.

Hi Ken,
I think you are right on. I had to stop for the day I was just so
frustrated. But, of course, I couldn’t stop thinking about and I
started wondering about PyCharm. I am pretty sure that I haven’t had
these problems when I have started everything from the command line in
Terminal. I will try that tomorrow. Plus, maybe I am not using PyCharm
correctly.

Are you still around? If so, could you look at this?

Looking at my post it seems that there is not much to go on in terms of understanding my problem. So, let me try to provide more detail.

I am in part 2 of the tutorial working with “models” (sqlite) and have written this code into "polls/models.py:

from django.db import models
from django.utils import timezone
import datetime

# 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

The indents are correct, just not shown in the markdown.

This is the shell command that brought the error:

Question.objects.get(pub_date_year=current_year)

Something is wrong with defining “current_year” and making it work. Can anyone see the problem?

Including the full and complete statement being entered along with the error message may help.

You’re trying to reference a field named pub_date_year - no such field exists. What you’re really trying to get to is the year component within the pub_date object, which is referenced with the double underscore, which means you want pub_date__year.

Ken

Thanks, but not sure what the “full and complete statement” means. The shell command is Question.objects.get(pub_date_year). I thought that this code would set up the the pub_date object access:

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()

I am referring to what you were entering in the shell, followed by the error message you received as a response.

The statement Question.objects.get(pub_date_year) isn’t valid syntactically, and doesn’t match any of the examples in the docs. So I’m just trying to establish what it is you’re trying to do, and then use that information to try and explain why you’re getting the error message you’re receiving.

If you print out the tutorial, which is the only way I can reference the reference, on page 8, about ½ way down is:
“>>> Question.objects.get(pub_date_year=current_date)”

Sorry, I truncated it in the “help” message, but that command produces the error:
FieldError: Cannot resolve keyword ‘pub_date_year’ into field. Choices are: choice, id, pub_date, question_text

Here is how the code looks on the tutorial page

def was_published_recently(self):
    return self.pub_date >+ timezone.now() - datetime.timedelta(days=1)

That formatting, which I don’t understand, produces a coding error, so I erased the dash and move the line datetime.timedelta(days=1) down two lines and left justified it.

I didn’t include that in the code snippet above. But, it’s two lines down and in line with “Class” above. That was the only way I could get the interpreter to accept it.

You’re trying to reference a field named pub_date_year - no such field exists. What you’re really trying to get to is the year component within the pub_date object, which is referenced with the double underscore , which means you want pub_date__year .

1 Like

Look at this! It worked.

maxim.z...@gmail.com

unread,

Aug 25, 2020, 3:11:20 PM (yesterday)

to django...@googlegroups.com

Hi, use double underscore before startswith and year:

In [9]: Question.objects.filter(question_text __ startswith =‘What’)

In [12]: Question.objects.get(pub_date __ year =current_year)

Now, that is not in the tutorial. How am I supposed to know that? How is any reader/student supposed to know that? Seems a bit frustrating.

Thanks to:
Best regards, Maxim Zemlyakov Email: maxim.z...@gmail.com GSM: +79032703210