How to use gpt to write programs and handle common errors in Django

At present, there are many AI assistants, and they are all very vague. They share a few commonly used ones such as poe.com, claude, and chathub Google plug-ins.
With so many little assistants, I personally think that prompt is very important. Learning how to use it is the key. The
following is an example of my program writing:
If you are a senior python development engineer, design a small program based on the django framework.
1. Log in to mysql and execute sql, and then display the query results.
2. Design three buttons to display the query sql results, export the current query results to csv, and then provide a download page, that is, the three buttons display 3
, The front-end is mainly simple and practical (refer to bootstrap)
, but it requires multiple corrections and verifications, and it may not be completed in one go.
Finished product:
Insert image description here
The following is the error report of the scene:
these three types of error reports are:

  1. ModuleNotFoundError: No module named 'mysql': This error is caused by views.pyimporting mysql.connectorthe module in but not installing it. You need to use pipthe installation mysql-connector-pythonmodule, which can be installed by running the following command in the terminal:

    pip install mysql-connector-python
    
  2. NameError: name 'os' is not defined: This error is because the module settings.pyis used in or other files os, but osthe module is not imported. You need to add the following import statements in the relevant files:

    import os
    
  3. django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.6.51): This error is because your Django project requires MySQL 8 or higher, but MySQL 5.6.51 is installed on your system. You can try upgrading MySQL to version 8 or higher to solve this problem.

tree:
Insert image description here
These files are the core components in the Django framework, each playing different roles:

  1. models.py

    • In Django, models.pya file defines the database model (Model), that is, the structure and fields of the database table.
    • By defining model classes and fields, Django can automatically generate the corresponding database table structure and provide a convenient API for interacting with the database.
    • Model classes usually inherit from django.db.models.Model, and each class's attributes correspond to a field in the database table.
  2. settings.py

    • settings.pyThe file contains the settings and configuration information for the Django project.
    • In this file, you can configure database connections, static file paths, internationalization settings, middleware, application lists, etc.
    • Modifications settings.pycan affect the behavior and functionality of the entire Django project.
  3. urls.py

    • urls.pyThe file defines a URL routing map that associates URLs with corresponding view functions.
    • Django's URL routing system will call the corresponding view function according to the URL requested by the user to handle the request.
    • In urls.py, you can define URL patterns and configure the mapping between URLs and view functions.
  4. views.py

    • views.pyThe file contains the view functions in the Django project.
    • View functions handle user requests, perform data processing, call models, render templates, etc., and then return the results to the user.
    • View functions usually receive user request objects (such as HttpRequest) and return an HttpResponse object.

In summary, models.pydefine the database model, settings.pyconfigure project settings, urls.pydefine URL mapping, views.pyhandle user requests and data processing. Together, these four files form the core functionality and architecture of the Django project.

Summary: It is a very good learning aid and can also be used as a search engine, but you need to make your own judgment or make more comparisons. Until you find the best and most correct answer

Guess you like

Origin blog.csdn.net/weixin_54104864/article/details/131974079