Class based view

Hi,

Following the very basic instruction at https://docs.djangoproject.com/en/3.2/topics/class-based-views/intro/, using an empty DJango project in Pycharm.

I have created a new Python file to house the basic class in the project folder:

from django.http import HttpResponse
from django.views import View

class MyView(View):
    def get(self, request):
        # <view logic>
        return HttpResponse('This is me and I am it')

but then in the urls.py file when I type in from DangoTutorial.views import MyView there is no “views” property or method of DJangoTutorial.

?

Hi.

Maybe your misspelling something in the from DangoTutorial.views import MyView line?

Can you show how’s your project structured?

Review the urls section of the Generic View portion of page 4 in the tutorial. If that doesn’t clear it up, please post your urls.py file here.

Thanks. I hadn’t actually completed the tutorial, so did that and yes that worked.

I’m not entirely sure that answers the original question, but I guess I’m getting ahead of myself. I think I can do most of what I need for now using these generic views and the ORM…Not sure, I’ve got to connect to a SQL server ultimately and that appears to be a brand new thing in DJango land.

On the point that Marco raised. I have seen many a response to a Python related question in places like Stack overflow along those lines. And given that Python is interpretted and I could be writing code in a text editor, that makes sense. I’m writing my code using the PyCharm Professional IDE, so that should warn me of these, yes?

There are three different perspectives on that:

  • Frequently (at least here - can’t say about SO), people retype what they’ve entered elsewhere, and introduce errors in transcribing from what they’ve done to what they post. So I’ve learned here to not necessarily assume that a typo actually exists in the code someone is trying to run.

  • IDEs, such as PyCharm (which I don’t use, so I can’t address it directly) should have some setting to flag non-existent imports. However, …

  • When you’re editing a file, no IDE can “know” that while a reference may not exist now, that it won’t exist at the time you start to run the code.

PyCharm has answered my question for me…it does and it does so inline, which is helpful. But then I must have pushed it too far and hit the very issue you’ve highlighted. I copied in some code from the tutorial (a display decorator) and that caused all sorts of issues. Removed the code…still issues. Closed the project, re-opened it, retyped the code manually and it all worked.

Lesson learned.