Home > other >  For help, python from entry to practice chapter 18
For help, python from entry to practice chapter 18

Time:10-05

This chapter, I learn to show 18.4.3, according to a particular topic page, always an error, compared the code also don't know where is wrong, please god help,
Shown earlier here, http://127.0.0.1:8000/topics/, is normal, it follows that some specific subject to error,

Noticed that the book is the address of the http://127.0.0.1:8000/topics/1/, and I was http://127.0.0.1:8000/topics/(% 3 fp1%5 CD +)/
Whether the path (' switchable viewer/? PThank you very much!

CodePudding user response:

Nobody seems to mention why entry without date_added properties, after half a day of testing, found the problem in views. Py inside
Def topic (request, topic_id) :
"" "displays a single topic and all the entries "" "
Topic=topic. Objects. The get (id=topic_id)
# entries=topic. Entry_set. Order_by (' - date_added)
Entries=topic. Entry_set. All ()
The context={' topic: topic, 'entries: entries}
Return render (request, 'learning_logs/topic. HTML, context)
entries=topic. Entry_set. Order_by (' - date_added) this sentence above, as long as the switch to entries=topic. Entry_set. All () , testing in the shell, entry no date_added attribute

Seems to have a little not science, but think one is so many predecessors have never mentioned this matter; Another is should be able to according to add time to sort, there is no such attributes and how to? What the problem is, also please great god solve, thank you!

CodePudding user response:

The same problem as I have seen your topic attribute a on_delete=less models. The CASCADE
You best all code is coming, see you
there is no other mistakes
"" "file models. Py "" "

The from the django. Db import models

# the Create your models here.

The class Topic (models. Model) :
"' user study theme" '

Text=models. CharField (max_length=200)
Date_added=models. DateTimeField (auto_now_add=True)

Def the __str__ (self) :
"" "return to model a string representation of the "" "
Return the self. The text

The class Entry (models. Model) :
Topic=models. ForeignKey (topic, on_delete=models. The CASCADE)
Text=models. TextField ()
Date_added=models. DateTimeField (auto_now_add=True)

The class Mete:
Verbose_name_plural='entries'

Def the __str__ (self) :
"" "return to model a string representation of the "" "
Return the self. The text [: 50] + '... '

CodePudding user response:


Path (' views. The index, name='index'),

Path (' switchable viewer/' views. Switchable viewer, name='switchable viewer'),

Path (" switchable viewer/& lt; Topic_id & gt;/", views. Topic name='topic')

CodePudding user response:

As a module in a path with a '/' if use '\' will appear such problems,
  • Related