Home > other >  [for] python programming: from entry to practice project 3 web applications appear TypeError ats/swi
[for] python programming: from entry to practice project 3 web applications appear TypeError ats/swi

Time:12-02

As shown in figure:


Urls. Py
 
"" "define learning_logs URL patterns "","

The from the django. Urls import path, re_path

From the import views

Urlpatterns=[
# page
Path (' views. The index, name='index'),

# show all theme
Path (' switchable viewer/' views. Switchable viewer, name='switchable viewer'),

# show all theme
Re_path (' switchable viewer/? P
# a particular theme detail page
Re_path (' switchable viewer/? P
# to add a new theme page
New_topic re_path ('/', views. New_topic, name='new_topic'),

# to add a new entry page
Re_path (' new_entry/? P]
App_name='learning_logs'




Views. Py
 
The from the django. Shortcuts import render
The from the django. HTTP import HttpResponseRedirect
The from the django. Urls import reverse

From the models import Topic
From the forms the import TopicForm, EntryForm

# the Create your views here.
Def index (request) :
"" "learning notes page "" "
Return render (request, 'learning_logs/index. HTML)

Def switchable viewer (request) :
"" the theme of the" show all "" "
Switchable viewer=Topic. Objects. Order_by (' date_added)
The context={' switchable viewer: switchable viewer}
Return render (request, 'learning_logs/switchable viewer. HTML, context)

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)
The context={' topic: topic, 'entries: entries}
Return render (request, 'learning_logs/topic. HTML, context)

Def new_topic (request) :
"" "to add a new theme "" "
if request.method ! :='POST'
# uncommitted data: create a new form
Form=TopicForm ()
The else:
# POST submission of data, the data processing
Form=TopicForm (request. POST)
If the form. Is_valid () :
Form. The save ()
Return HttpResponseRedirect (reverse (' learning_logs: switchable viewer '))

The context={' form: form}
Return render (request, 'learning_logs/new_topic. HTML, context)

Def new_entry (request, topic_id) :
"" "add a new entry in the specific theme "" "
Topic=topic. Objects. The get (id=topic_id)

if request.method ! :='POST'
# uncommitted data, create an empty form
Form=EntryForm ()
The else:
# POST submission of data, the data processing
Form=EntryForm (data=https://bbs.csdn.net/topics/request.POST)
If the form. Is_valid () :
New_entry=form. The save (commit=False)
New_entry. Topic=topic
New_entry. The save ()
Return HttpResponseRedirect (reverse (' learning_logs: topic, args=[topic_id]))

The context={' topic: topic, 'form: form}
Return render (request, 'learning_logs/new_entry. HTML, context)

Switchable viewer. HTML


 {% extends "learning_logs/base. HTML" %} 

{% block content %}

Topics







{% endblock content %}


Topic. The HTML
 {% extends "learning_logs/base. HTML" %} 

{% block content %}

Topic: {{Topic}} & lt;/p>

Entries: & lt;/p>





    {% for entry in the entries %}

  • {{entry. Date_added | date: 'M d, Y H: I'}} & lt;/p>

    {{entry. The text | linebreaks}} & lt;/p>


  • {% empty %}

  • There are no entries for this topic yet.

  • {% endfor %}



{% endblock content %}


Other pages can also into, is can't get into a particular theme interface
For help, ah, don't want to own a python career is a small BUG, broken cry breaks down
Python version is 3.6, Django 2.0

CodePudding user response:

If I'm not wrong
# show all theme
Re_path (' switchable viewer/? P
# a particular theme detail page
Re_path (' switchable viewer/? PYou two url is the same as regular, may be there is something wrong with it,
I guess, a particular theme is regular the topic/detail page (? P... ?

CodePudding user response:

# show all theme
Re_path (' switchable viewer/? P CodePudding user response:

Did you find out why? I and you the same question
  • Related